Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.6.0+

S3: List More Than 1,000 Objects with Pagination

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Uses S3_ListBucketObjects with list-type=2 to request ListObjectsV2. Follows opaque NextContinuationToken values while IsTruncated is true, including empty pages; detects missing/repeated tokens and stops at 10,000 pages with incomplete-results status. Requires s3:ListBucket.

Requests encoding-type=url so keys containing XML-incompatible characters can be represented. Decode returned keys exactly once before reusing them. Query values are encoded separately. Sizes remain decimal strings to preserve large values; LastModified is an ISO 8601 UTC timestamp, not RFC 822. Owner.DisplayName is not used. Listing pages are not a single snapshot if objects change during enumeration.

AWS reference

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  This example requires the Chilkat API to have been previously unlocked.
;  See Global Unlock Sample for sample code.
;  Load private credentials; never put access keys or session tokens in source.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private S3 credentials." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("access_key"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bStringMatch1 = True)) Then
    ConsoleWrite("Missing or invalid access_key." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bStringMatch2 = True)) Then
    ConsoleWrite("Missing or invalid secret_key." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AwsAccessKey = $oCredentials.StringOf("access_key")
$oHttp.AwsSecretKey = $oCredentials.StringOf("secret_key")
If ($oCredentials.HasMember("session_token") = True) Then
    $oStringCompare.SetString($oCredentials.StringOf("session_token"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
    If (($oCredentials.JsonTypeOf("session_token") <> 1) Or ($bStringMatch3 = True)) Then
        ConsoleWrite("session_token must be a nonempty string when supplied." & @CRLF)
        Exit
    EndIf

    $oHttp.AwsSessionToken = $oCredentials.StringOf("session_token")
EndIf

;  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
;  Set the actual bucket region, not the caller's location.
Local $sRegion = "us-west-2"
$oHttp.AwsRegion = $sRegion
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("s3.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oHttp.AwsEndpoint = $oEndpoint.GetAsString()
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
;  A region redirect is not success. Correct the endpoint and signing region.
Local $sBucketName = "your-bucket-name"
$oBucketPath = ObjCreate("Chilkat.StringBuilder")
$oEncoded = ObjCreate("Chilkat.StringBuilder")
$oXml = ObjCreate("Chilkat.Xml")
$oSeenTokens = ObjCreate("Chilkat.Hashtable")
Local $sToken = ""
Local $bMore = True
Local $iPage = 0
While ($bMore = True)
    If ($iPage = 10000) Then
        ConsoleWrite("Page limit reached; listing is incomplete." & @CRLF)
        Exit
    EndIf

    $iPage = $iPage + 1
    $oBucketPath.SetString($sBucketName)
    $oBucketPath.Append("?list-type=2&max-keys=1000&encoding-type=url")
    $oStringCompare.SetString($sToken)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("",True)
    If ($bStringMatch4 <> True) Then
        $oEncoded.SetString($sToken)
        $oBucketPath.Append("&continuation-token=")
        $oBucketPath.Append($oEncoded.GetEncoded("url","utf-8"))
    EndIf

Local $sXmlStr = $oHttp.S3_ListBucketObjects($oBucketPath.GetAsString())
    If ($oHttp.LastMethodSuccess <> True) Then
        ConsoleWrite("Request failed (transport or S3 error). HTTP status: " & $oHttp.LastStatus & @CRLF)
        ;  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        Exit
    EndIf

    If ($oHttp.LastStatus <> 200) Then
        ConsoleWrite("Unexpected HTTP status: " & $oHttp.LastStatus & @CRLF)
        Exit
    EndIf

    $bSuccess = $oXml.LoadXml($sXmlStr)
    If ($bSuccess <> True) Then
        ConsoleWrite("Invalid XML response." & @CRLF)
        Exit
    EndIf

    $oStringCompare.SetString($oXml.Tag)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("ListBucketResult",True)
    If ($bStringMatch5 <> True) Then
        ConsoleWrite("Unexpected XML response root." & @CRLF)
        Exit
    EndIf

    $oStringCompare.SetString($oXml.GetChildContent("EncodingType"))
Local $bStringMatch6 = $oStringCompare.ContentsEqual("url",True)
    If ($bStringMatch6 <> True) Then
        ConsoleWrite("Expected URL-encoded listing keys." & @CRLF)
        Exit
    EndIf

Local $i = 0
Local $iCount = $oXml.NumChildrenHavingTag("Contents")
    While ($i < $iCount)
        $oXml.I = $i
        ;  Decode keys exactly once. Sizes remain decimal strings (not int).
Local $sItemKey = $oHttp.UrlDecode($oXml.GetChildContent("Contents[i]|Key"))
        $oStringCompare.SetString($sItemKey)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("",True)
        If (($oHttp.LastMethodSuccess <> True) Or ($bStringMatch7 = True)) Then
            ConsoleWrite("Invalid object key." & @CRLF)
            Exit
        EndIf

Local $size = $oXml.GetChildContent("Contents[i]|Size")
        $oStringCompare.SetString($size)
Local $bStringMatch8 = $oStringCompare.ContentsEqual("",True)
        If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch8 = True)) Then
            ConsoleWrite("Incomplete object entry." & @CRLF)
            Exit
        EndIf

Local $sModified = $oXml.GetChildContent("Contents[i]|LastModified")
        $oStringCompare.SetString($sModified)
Local $bStringMatch9 = $oStringCompare.ContentsEqual("",True)
        If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch9 = True)) Then
            ConsoleWrite("Incomplete object entry." & @CRLF)
            Exit
        EndIf

        ConsoleWrite("Key: " & $sItemKey & @CRLF)
        ConsoleWrite("Bytes: " & $size & @CRLF)
        ;  S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
        ConsoleWrite("Last modified (UTC): " & $sModified & @CRLF)
        $i = $i + 1
    Wend
Local $sTruncated = $oXml.GetChildContent("IsTruncated")
    $oStringCompare.SetString($sTruncated)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("true",True)
    $oStringCompare.SetString($sTruncated)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("false",True)
    If (($bStringMatch10 <> True) And ($bStringMatch11 <> True)) Then
        ConsoleWrite("Missing or invalid IsTruncated; listing is incomplete." & @CRLF)
        Exit
    EndIf

    $bMore = False
    $oStringCompare.SetString($sTruncated)
Local $bStringMatch12 = $oStringCompare.ContentsEqual("true",True)
    If ($bStringMatch12 = True) Then
        $sToken = $oXml.GetChildContent("NextContinuationToken")
        $oStringCompare.SetString($sToken)
Local $bStringMatch13 = $oStringCompare.ContentsEqual("",True)
        If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch13 = True)) Then
            ConsoleWrite("Missing continuation token; listing is incomplete." & @CRLF)
            Exit
        EndIf

        If ($oSeenTokens.Contains($sToken) = True) Then
            ConsoleWrite("Repeated continuation token; listing is incomplete." & @CRLF)
            Exit
        EndIf

        $bSuccess = $oSeenTokens.AddStr($sToken,"seen")
        If ($bSuccess <> True) Then
            ConsoleWrite("Could not track pagination." & @CRLF)
            Exit
        EndIf

        $bMore = True
    EndIf

Wend
ConsoleWrite("Object listing complete." & @CRLF)