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

S3: List Objects with ListObjectsV2

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkHashtable.pb"

Procedure ChilkatExample()

    success.i = 0

    stringCompare.i = CkStringBuilder::ckCreate()
    If stringCompare.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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.
    credentials.i = CkJsonObject::ckCreate()
    If credentials.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(credentials, 1)
    success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/s3.json")
    If success <> 1
        Debug "Could not load private S3 credentials."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"access_key"))
    stringMatch1.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (stringMatch1 = 1)
        Debug "Missing or invalid access_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"secret_key"))
    stringMatch2.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (stringMatch2 = 1)
        Debug "Missing or invalid secret_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAwsAccessKey(http, CkJsonObject::ckStringOf(credentials,"access_key"))
    CkHttp::setCkAwsSecretKey(http, CkJsonObject::ckStringOf(credentials,"secret_key"))
    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"session_token"))
        stringMatch3.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
        If (CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1) OR (stringMatch3 = 1)
            Debug "session_token must be a nonempty string when supplied."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            ProcedureReturn
        EndIf

        CkHttp::setCkAwsSessionToken(http, CkJsonObject::ckStringOf(credentials,"session_token"))
    EndIf

    ;  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
    ;  Set the actual bucket region, not the caller's location.
    region.s = "us-west-2"
    CkHttp::setCkAwsRegion(http, region)
    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(endpoint,"s3.")
    CkStringBuilder::ckAppend(endpoint,region)
    CkStringBuilder::ckAppend(endpoint,".amazonaws.com")
    CkHttp::setCkAwsEndpoint(http, CkStringBuilder::ckGetAsString(endpoint))
    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkFollowRedirects(http, 0)
    ;  A region redirect is not success. Correct the endpoint and signing region.
    bucketName.s = "your-bucket-name"
    bucketPath.i = CkStringBuilder::ckCreate()
    If bucketPath.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    encoded.i = CkStringBuilder::ckCreate()
    If encoded.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    seenTokens.i = CkHashtable::ckCreate()
    If seenTokens.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    token.s = ""
    more.i = 1
    page.i = 0
    While (more = 1)
        If page = 10000
            Debug "Page limit reached; listing is incomplete."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        page = page + 1
        CkStringBuilder::ckSetString(bucketPath,bucketName)
        CkStringBuilder::ckAppend(bucketPath,"?list-type=2&max-keys=1000&encoding-type=url")
        CkStringBuilder::ckSetString(stringCompare,token)
        stringMatch4.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
        If stringMatch4 <> 1
            CkStringBuilder::ckSetString(encoded,token)
            CkStringBuilder::ckAppend(bucketPath,"&continuation-token=")
            CkStringBuilder::ckAppend(bucketPath,CkStringBuilder::ckGetEncoded(encoded,"url","utf-8"))
        EndIf

        xmlStr.s = CkHttp::ckS3_ListBucketObjects(http,CkStringBuilder::ckGetAsString(bucketPath))
        If CkHttp::ckLastMethodSuccess(http) <> 1
            Debug "Request failed (transport or S3 error). HTTP status: " + Str(CkHttp::ckLastStatus(http))
            ;  Inspect LastErrorText privately. Do not log credentials or signed URLs.
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        If CkHttp::ckLastStatus(http) <> 200
            Debug "Unexpected HTTP status: " + Str(CkHttp::ckLastStatus(http))
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        success = CkXml::ckLoadXml(xml,xmlStr)
        If success <> 1
            Debug "Invalid XML response."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(xml))
        stringMatch5.i = CkStringBuilder::ckContentsEqual(stringCompare,"ListBucketResult",1)
        If stringMatch5 <> 1
            Debug "Unexpected XML response root."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        CkStringBuilder::ckSetString(stringCompare,CkXml::ckGetChildContent(xml,"EncodingType"))
        stringMatch6.i = CkStringBuilder::ckContentsEqual(stringCompare,"url",1)
        If stringMatch6 <> 1
            Debug "Expected URL-encoded listing keys."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        i.i = 0
        count.i = CkXml::ckNumChildrenHavingTag(xml,"Contents")
        While (i < count)
            CkXml::setCkI(xml, i)
            ;  Decode keys exactly once. Sizes remain decimal strings (not int).
            itemKey.s = CkHttp::ckUrlDecode(http,CkXml::ckGetChildContent(xml,"Contents[i]|Key"))
            CkStringBuilder::ckSetString(stringCompare,itemKey)
            stringMatch7.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
            If (CkHttp::ckLastMethodSuccess(http) <> 1) OR (stringMatch7 = 1)
                Debug "Invalid object key."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            size.s = CkXml::ckGetChildContent(xml,"Contents[i]|Size")
            CkStringBuilder::ckSetString(stringCompare,size)
            stringMatch8.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
            If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch8 = 1)
                Debug "Incomplete object entry."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            modified.s = CkXml::ckGetChildContent(xml,"Contents[i]|LastModified")
            CkStringBuilder::ckSetString(stringCompare,modified)
            stringMatch9.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
            If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch9 = 1)
                Debug "Incomplete object entry."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            Debug "Key: " + itemKey
            Debug "Bytes: " + size
            ;  S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
            Debug "Last modified (UTC): " + modified
            i = i + 1
        Wend
        truncated.s = CkXml::ckGetChildContent(xml,"IsTruncated")
        CkStringBuilder::ckSetString(stringCompare,truncated)
        stringMatch10.i = CkStringBuilder::ckContentsEqual(stringCompare,"true",1)
        CkStringBuilder::ckSetString(stringCompare,truncated)
        stringMatch11.i = CkStringBuilder::ckContentsEqual(stringCompare,"false",1)
        If (stringMatch10 <> 1) AND (stringMatch11 <> 1)
            Debug "Missing or invalid IsTruncated; listing is incomplete."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkHttp::ckDispose(http)
            CkStringBuilder::ckDispose(endpoint)
            CkStringBuilder::ckDispose(bucketPath)
            CkStringBuilder::ckDispose(encoded)
            CkXml::ckDispose(xml)
            CkHashtable::ckDispose(seenTokens)
            ProcedureReturn
        EndIf

        more = 0
        CkStringBuilder::ckSetString(stringCompare,truncated)
        stringMatch12.i = CkStringBuilder::ckContentsEqual(stringCompare,"true",1)
        If stringMatch12 = 1
            token = CkXml::ckGetChildContent(xml,"NextContinuationToken")
            CkStringBuilder::ckSetString(stringCompare,token)
            stringMatch13.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
            If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch13 = 1)
                Debug "Missing continuation token; listing is incomplete."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            If CkHashtable::ckContains(seenTokens,token) = 1
                Debug "Repeated continuation token; listing is incomplete."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            success = CkHashtable::ckAddStr(seenTokens,token,"seen")
            If success <> 1
                Debug "Could not track pagination."
                CkStringBuilder::ckDispose(stringCompare)
                CkJsonObject::ckDispose(credentials)
                CkHttp::ckDispose(http)
                CkStringBuilder::ckDispose(endpoint)
                CkStringBuilder::ckDispose(bucketPath)
                CkStringBuilder::ckDispose(encoded)
                CkXml::ckDispose(xml)
                CkHashtable::ckDispose(seenTokens)
                ProcedureReturn
            EndIf

            more = 1
        EndIf

    Wend
    Debug "Object listing complete."


    CkStringBuilder::ckDispose(stringCompare)
    CkJsonObject::ckDispose(credentials)
    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(endpoint)
    CkStringBuilder::ckDispose(bucketPath)
    CkStringBuilder::ckDispose(encoded)
    CkXml::ckDispose(xml)
    CkHashtable::ckDispose(seenTokens)


    ProcedureReturn
EndProcedure