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

S3: List Objects Under a Prefix

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.

A prefix is a key-name filter, not a filesystem folder. The trailing slash matters: images/ excludes keys such as images-old/file.jpg. Without a delimiter, all matching descendant keys are included.

AWS reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoStringCompare
    Handle hoCredentials
    Boolean iStringMatch1
    Boolean iStringMatch2
    Handle hoHttp
    Boolean iStringMatch3
    String sRegion
    Handle hoEndpoint
    String sBucketName
    Handle hoBucketPath
    Handle hoEncoded
    Handle hoXml
    Handle hoSeenTokens
    String sToken
    Boolean iMore
    Integer iPage
    String sPrefix
    Boolean iStringMatch4
    String sXmlStr
    Boolean iStringMatch5
    Boolean iStringMatch6
    Integer i
    Integer iCount
    String sItemKey
    Boolean iStringMatch7
    String sSize
    Boolean iStringMatch8
    String sModified
    Boolean iStringMatch9
    String sTruncated
    Boolean iStringMatch10
    Boolean iStringMatch11
    Boolean iStringMatch12
    Boolean iStringMatch13
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
    If (Not(IsComObjectCreated(hoStringCompare))) Begin
        Send CreateComObject of hoStringCompare
    End
    //  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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
    If (Not(IsComObjectCreated(hoCredentials))) Begin
        Send CreateComObject of hoCredentials
    End
    Set ComStrict Of hoCredentials To True
    Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private S3 credentials."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
    Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
        Showln "Missing or invalid access_key."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
    Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
        Showln "Missing or invalid secret_key."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get ComStringOf Of hoCredentials "access_key" To sTemp1
    Set ComAwsAccessKey Of hoHttp To sTemp1
    Get ComStringOf Of hoCredentials "secret_key" To sTemp1
    Set ComAwsSecretKey Of hoHttp To sTemp1
    Get ComHasMember Of hoCredentials "session_token" To bTemp1
    If (bTemp1 = True) Begin
        Get ComStringOf Of hoCredentials "session_token" To sTemp1
        Get ComSetString Of hoStringCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
        Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
        If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
            Showln "session_token must be a nonempty string when supplied."
            Procedure_Return
        End

        Get ComStringOf Of hoCredentials "session_token" To sTemp1
        Set ComAwsSessionToken Of hoHttp To sTemp1
    End

    //  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
    //  Set the actual bucket region, not the caller's location.
    Move "us-west-2" To sRegion
    Set ComAwsRegion Of hoHttp To sRegion
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComAppend Of hoEndpoint "s3." To iSuccess
    Get ComAppend Of hoEndpoint sRegion To iSuccess
    Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
    Get ComGetAsString Of hoEndpoint To sTemp1
    Set ComAwsEndpoint Of hoHttp To sTemp1
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    //  A region redirect is not success. Correct the endpoint and signing region.
    Move "your-bucket-name" To sBucketName
    Get Create (RefClass(cComChilkatStringBuilder)) To hoBucketPath
    If (Not(IsComObjectCreated(hoBucketPath))) Begin
        Send CreateComObject of hoBucketPath
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEncoded
    If (Not(IsComObjectCreated(hoEncoded))) Begin
        Send CreateComObject of hoEncoded
    End
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get Create (RefClass(cComChilkatHashtable)) To hoSeenTokens
    If (Not(IsComObjectCreated(hoSeenTokens))) Begin
        Send CreateComObject of hoSeenTokens
    End
    Move "" To sToken
    Move True To iMore
    Move 0 To iPage
    Move "images/" To sPrefix
    While (iMore = True)
        If (iPage = 10000) Begin
            Showln "Page limit reached; listing is incomplete."
            Procedure_Return
        End

        Move (iPage + 1) To iPage
        Get ComSetString Of hoBucketPath sBucketName To iSuccess
        Get ComAppend Of hoBucketPath "?list-type=2&max-keys=1000&encoding-type=url" To iSuccess
        Get ComSetString Of hoEncoded sPrefix To iSuccess
        Get ComAppend Of hoBucketPath "&prefix=" To iSuccess
        Get ComGetEncoded Of hoEncoded "url" "utf-8" To sTemp1
        Get ComAppend Of hoBucketPath sTemp1 To iSuccess
        Get ComSetString Of hoStringCompare sToken To iSuccess
        Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
        If (iStringMatch4 <> True) Begin
            Get ComSetString Of hoEncoded sToken To iSuccess
            Get ComAppend Of hoBucketPath "&continuation-token=" To iSuccess
            Get ComGetEncoded Of hoEncoded "url" "utf-8" To sTemp1
            Get ComAppend Of hoBucketPath sTemp1 To iSuccess
        End

        Get ComGetAsString Of hoBucketPath To sTemp1
        Get ComS3_ListBucketObjects Of hoHttp sTemp1 To sXmlStr
        Get ComLastMethodSuccess Of hoHttp To bTemp1
        If (bTemp1 <> True) Begin
            Get ComLastStatus Of hoHttp To iTemp1
            Showln "Request failed (transport or S3 error). HTTP status: " iTemp1
            //  Inspect LastErrorText privately. Do not log credentials or signed URLs.
            Procedure_Return
        End

        Get ComLastStatus Of hoHttp To iTemp1
        If (iTemp1 <> 200) Begin
            Get ComLastStatus Of hoHttp To iTemp1
            Showln "Unexpected HTTP status: " iTemp1
            Procedure_Return
        End

        Get ComLoadXml Of hoXml sXmlStr To iSuccess
        If (iSuccess <> True) Begin
            Showln "Invalid XML response."
            Procedure_Return
        End

        Get ComTag Of hoXml To sTemp1
        Get ComSetString Of hoStringCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoStringCompare "ListBucketResult" True To iStringMatch5
        If (iStringMatch5 <> True) Begin
            Showln "Unexpected XML response root."
            Procedure_Return
        End

        Get ComGetChildContent Of hoXml "EncodingType" To sTemp1
        Get ComSetString Of hoStringCompare sTemp1 To iSuccess
        Get ComContentsEqual Of hoStringCompare "url" True To iStringMatch6
        If (iStringMatch6 <> True) Begin
            Showln "Expected URL-encoded listing keys."
            Procedure_Return
        End

        Move 0 To i
        Get ComNumChildrenHavingTag Of hoXml "Contents" To iCount
        While (i < iCount)
            Set ComI Of hoXml To i
            //  Decode keys exactly once. Sizes remain decimal strings (not int).
            Get ComGetChildContent Of hoXml "Contents[i]|Key" To sTemp1
            Get ComUrlDecode Of hoHttp sTemp1 To sItemKey
            Get ComSetString Of hoStringCompare sItemKey To iSuccess
            Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
            Get ComLastMethodSuccess Of hoHttp To bTemp1
            If ((bTemp1 <> True) Or (iStringMatch7 = True)) Begin
                Showln "Invalid object key."
                Procedure_Return
            End

            Get ComGetChildContent Of hoXml "Contents[i]|Size" To sSize
            Get ComSetString Of hoStringCompare sSize To iSuccess
            Get ComContentsEqual Of hoStringCompare "" True To iStringMatch8
            Get ComLastMethodSuccess Of hoXml To bTemp1
            If ((bTemp1 <> True) Or (iStringMatch8 = True)) Begin
                Showln "Incomplete object entry."
                Procedure_Return
            End

            Get ComGetChildContent Of hoXml "Contents[i]|LastModified" To sModified
            Get ComSetString Of hoStringCompare sModified To iSuccess
            Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
            Get ComLastMethodSuccess Of hoXml To bTemp1
            If ((bTemp1 <> True) Or (iStringMatch9 = True)) Begin
                Showln "Incomplete object entry."
                Procedure_Return
            End

            Showln "Key: " sItemKey
            Showln "Bytes: " sSize
            //  S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
            Showln "Last modified (UTC): " sModified
            Move (i + 1) To i
        Loop

        Get ComGetChildContent Of hoXml "IsTruncated" To sTruncated
        Get ComSetString Of hoStringCompare sTruncated To iSuccess
        Get ComContentsEqual Of hoStringCompare "true" True To iStringMatch10
        Get ComSetString Of hoStringCompare sTruncated To iSuccess
        Get ComContentsEqual Of hoStringCompare "false" True To iStringMatch11
        If ((iStringMatch10 <> True) And (iStringMatch11 <> True)) Begin
            Showln "Missing or invalid IsTruncated; listing is incomplete."
            Procedure_Return
        End

        Move False To iMore
        Get ComSetString Of hoStringCompare sTruncated To iSuccess
        Get ComContentsEqual Of hoStringCompare "true" True To iStringMatch12
        If (iStringMatch12 = True) Begin
            Get ComGetChildContent Of hoXml "NextContinuationToken" To sToken
            Get ComSetString Of hoStringCompare sToken To iSuccess
            Get ComContentsEqual Of hoStringCompare "" True To iStringMatch13
            Get ComLastMethodSuccess Of hoXml To bTemp1
            If ((bTemp1 <> True) Or (iStringMatch13 = True)) Begin
                Showln "Missing continuation token; listing is incomplete."
                Procedure_Return
            End

            Get ComContains Of hoSeenTokens sToken To bTemp1
            If (bTemp1 = True) Begin
                Showln "Repeated continuation token; listing is incomplete."
                Procedure_Return
            End

            Get ComAddStr Of hoSeenTokens sToken "seen" To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not track pagination."
                Procedure_Return
            End

            Move True To iMore
        End

    Loop

    Showln "Object listing complete."


End_Procedure