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

S3: List All Buckets with STS Credentials

See more AWS Security Token Service Examples

Uses all three temporary credential fields from the private XML file produced by either GetSessionToken or AssumeRole. Validates the response root, nonempty credential fields and ISO 8601 expiration before connecting, then checks expiry with a 60-second margin before each page. Does not print credential data, renew it automatically, or infer freshness merely from the presence of a file.

Uses Chilkat.Rest/AuthAws with Signature Version 4, service s3, explicit us-west-2 signing region, a matching regional endpoint, and TLS certificate verification. STS issuance region does not fix the service-request signing region. The session token is sent in the signed query on every request; do not log the request URL. Configure the appropriate endpoint for other partitions.

Requests max-buckets=100 and follows S3's ContinuationToken, including after an empty page. Tokens are opaque, repeated tokens stop enumeration, and a 10,000-page bound reports incomplete results. Requires s3:ListAllMyBuckets. Lists general purpose buckets owned by the authenticated account, not every bucket the caller can access. Directory buckets are excluded. Does not depend on Owner.DisplayName. Pagination is important for accounts with elevated bucket quotas, where unpaginated listing is rejected.

Pre-protect the credential file and output environment. This read-only consumer does not change the XML file. A production credential provider should coordinate refresh and publication of a complete credential set rather than partially updating it while requests are running.

AWS API reference

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoStringCompare
Token    Handle hoXmlToken
    String sCredentialPath
    Boolean iStringMatch1
    Boolean iStringMatch2
    Boolean iStringMatch3
    String sAccessKey
    String sSecretKey
    String sSessionToken
    String sExpiration
    Handle hoPath
    Boolean iStringMatch4
    Boolean iStringMatch5
    Boolean iStringMatch6
    Boolean iStringMatch7
    Handle hoExpires
    Variant vNow
    Handle hoNow
    String sRegion
    Handle hoEndpoint
    Variant vAws
    Handle hoAws
    Handle hoRest
    Variant vConnection
    Handle hoConnection
    Handle hoXml
    String sValue
    Handle hoSeenTokens
    String sContinuation
    String sResponseXml
    Integer iPage
    Integer iIndex
    Boolean iMore
    Boolean iStringMatch8
    Boolean iStringMatch9
    Boolean iStringMatch10
    Boolean iStringMatch11
    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
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Accept the private XML handoff from either GetSessionToken or AssumeRole.
    Get Create (RefClass(cComChilkatXml)) To hoXmlToken
    If (Not(IsComObjectCreated(hoXmlToken))) Begin
        Send CreateComObject of hoXmlToken
    End
    Set ComStrict Of hoXmlToken To True
    Get ComLoadXmlFile Of hoXmlToken "qa_data/tokens/aws_session_token.xml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load the private STS credential file."
        Procedure_Return
    End

    Move "" To sCredentialPath
    Get ComTag Of hoXmlToken To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "GetSessionTokenResponse" True To iStringMatch1
    If (iStringMatch1 = True) Begin
        Move "GetSessionTokenResult|Credentials" To sCredentialPath
    End

    Get ComTag Of hoXmlToken To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "AssumeRoleResponse" True To iStringMatch2
    If (iStringMatch2 = True) Begin
        Move "AssumeRoleResult|Credentials" To sCredentialPath
    End

    Get ComSetString Of hoStringCompare sCredentialPath To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
    If (iStringMatch3 = True) Begin
        Showln "Expected a GetSessionToken or AssumeRole credential response."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoPath
    If (Not(IsComObjectCreated(hoPath))) Begin
        Send CreateComObject of hoPath
    End
    Get ComSetString Of hoPath sCredentialPath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComAppend Of hoPath "|AccessKeyId" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComGetAsString Of hoPath To sTemp1
    Get ComGetChildContent Of hoXmlToken sTemp1 To sAccessKey
    Get ComSetString Of hoStringCompare sAccessKey To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
    Get ComLastMethodSuccess Of hoXmlToken To bTemp1
    If ((bTemp1 <> True) Or (iStringMatch4 = True)) Begin
        Showln "Missing or empty AccessKeyId."
        Procedure_Return
    End

    Get ComSetString Of hoPath sCredentialPath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComAppend Of hoPath "|SecretAccessKey" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComGetAsString Of hoPath To sTemp1
    Get ComGetChildContent Of hoXmlToken sTemp1 To sSecretKey
    Get ComSetString Of hoStringCompare sSecretKey To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
    Get ComLastMethodSuccess Of hoXmlToken To bTemp1
    If ((bTemp1 <> True) Or (iStringMatch5 = True)) Begin
        Showln "Missing or empty SecretAccessKey."
        Procedure_Return
    End

    Get ComSetString Of hoPath sCredentialPath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComAppend Of hoPath "|SessionToken" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComGetAsString Of hoPath To sTemp1
    Get ComGetChildContent Of hoXmlToken sTemp1 To sSessionToken
    Get ComSetString Of hoStringCompare sSessionToken To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
    Get ComLastMethodSuccess Of hoXmlToken To bTemp1
    If ((bTemp1 <> True) Or (iStringMatch6 = True)) Begin
        Showln "Missing or empty SessionToken."
        Procedure_Return
    End

    Get ComSetString Of hoPath sCredentialPath To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComAppend Of hoPath "|Expiration" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not build credential path."
        Procedure_Return
    End

    Get ComGetAsString Of hoPath To sTemp1
    Get ComGetChildContent Of hoXmlToken sTemp1 To sExpiration
    Get ComSetString Of hoStringCompare sExpiration To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
    Get ComLastMethodSuccess Of hoXmlToken To bTemp1
    If ((bTemp1 <> True) Or (iStringMatch7 = True)) Begin
        Showln "Missing or empty Expiration."
        Procedure_Return
    End

    Get Create (RefClass(cComCkDateTime)) To hoExpires
    If (Not(IsComObjectCreated(hoExpires))) Begin
        Send CreateComObject of hoExpires
    End
    Get Create (RefClass(cComCkDateTime)) To hoNow
    If (Not(IsComObjectCreated(hoNow))) Begin
        Send CreateComObject of hoNow
    End
    Get ComSetFromTimestamp Of hoExpires sExpiration To iSuccess
    If (iSuccess <> True) Begin
        Showln "Stored expiration is invalid."
        Procedure_Return
    End

    Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read current time."
        Procedure_Return
    End

    Get ComDiffSeconds Of hoExpires     Get pvComObject of hoNow to vNow
vNow To iTemp1
    If (iTemp1 <= 60) Begin
        Showln "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
        Procedure_Return
    End

    //  This S3 region is independent of the region that issued the STS credentials.
    Move "us-west-2" 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 Create (RefClass(cComChilkatAuthAws)) To hoAws
    If (Not(IsComObjectCreated(hoAws))) Begin
        Send CreateComObject of hoAws
    End
    Set ComAccessKey Of hoAws To sAccessKey
    Set ComSecretKey Of hoAws To sSecretKey
    Set ComRegion Of hoAws To sRegion
    Set ComServiceName Of hoAws To "s3"
    Set ComSignatureVersion Of hoAws To 4
    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Set ComConnectTimeoutMs Of hoRest To 20000
    Set ComIdleTimeoutMs Of hoRest To 60000
    Set ComVerboseLogging Of hoRest To False
    Get pvComObject of hoAws to vAws
    Get ComSetAuthAws Of hoRest vAws To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not configure request signing."
        Procedure_Return
    End

    //  Keep this verified Socket alive while Rest uses its connection.
    Get Create (RefClass(cComChilkatSocket)) To hoConnection
    If (Not(IsComObjectCreated(hoConnection))) Begin
        Send CreateComObject of hoConnection
    End
    Set ComRequireSslCertVerify Of hoConnection To True
    Set ComMaxReadIdleMs Of hoConnection To 60000
    Set ComMaxSendIdleMs Of hoConnection To 60000
    Get ComGetAsString Of hoEndpoint To sTemp1
    Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "TLS connection failed; inspect diagnostics privately."
        Procedure_Return
    End

    Get pvComObject of hoConnection to vConnection
    Get ComUseConnection Of hoRest vConnection True To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not attach TLS connection."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComStrict Of hoXml To True

    Get Create (RefClass(cComChilkatHashtable)) To hoSeenTokens
    If (Not(IsComObjectCreated(hoSeenTokens))) Begin
        Send CreateComObject of hoSeenTokens
    End
    Move "" To sContinuation

    Move 0 To iPage

    Move True To iMore
    While (iMore = True)
        If (iPage = 10000) Begin
            Showln "Page bound reached; bucket enumeration is incomplete."
            Procedure_Return
        End

        Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not read current time."
            Procedure_Return
        End

        Get ComDiffSeconds Of hoExpires         Get pvComObject of hoNow to vNow
vNow To iTemp1
        If (iTemp1 <= 60) Begin
            Showln "Credentials are expiring; obtain fresh credentials before continuing the listing."
            Procedure_Return
        End

        Move (iPage + 1) To iPage
        Get ComClearAllQueryParams Of hoRest To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not clear parameters from the previous page."
            Procedure_Return
        End

        //  All three credential components are required. Never log this signed URL.
        Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sSessionToken To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not set X-Amz-Security-Token."
            Procedure_Return
        End

        Get ComAddQueryParam Of hoRest "max-buckets" "100" To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not set max-buckets."
            Procedure_Return
        End

        Get ComSetString Of hoStringCompare sContinuation To iSuccess
        Get ComContentsEqual Of hoStringCompare "" True To iStringMatch8
        If (iStringMatch8 <> True) Begin
            Get ComAddQueryParam Of hoRest "continuation-token" sContinuation To iSuccess
            If (iSuccess <> True) Begin
                Showln "Could not set continuation-token."
                Procedure_Return
            End

        End

        Get ComFullRequestNoBody Of hoRest "GET" "/" To sResponseXml
        Get ComLastMethodSuccess Of hoRest To bTemp1
        If (bTemp1 <> True) Begin
            Showln "S3 request did not complete; enumeration is incomplete."
            Procedure_Return
        End

        Get ComLoadXml Of hoXml sResponseXml To iSuccess
        Get ComResponseStatusCode Of hoRest To iTemp1
        If (iTemp1 <> 200) Begin
            Get ComResponseStatusCode Of hoRest To iTemp1
            Showln "S3 HTTP status: " iTemp1
            If (iSuccess = True) Begin
                Get ComGetChildContent Of hoXml "Code" To sValue
                Get ComLastMethodSuccess Of hoXml To bTemp1
                If (bTemp1 = True) Begin
                    Showln "AWS error code: " sValue
                End

                Get ComGetChildContent Of hoXml "RequestId" To sValue
                Get ComLastMethodSuccess Of hoXml To bTemp1
                If (bTemp1 = True) Begin
                    Showln "AWS request ID: " sValue
                End

            End

            Procedure_Return
        End

        If (iSuccess <> True) Begin
            Showln "Invalid S3 XML response."
            Procedure_Return
        End

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

        Get ComHasChildWithTag Of hoXml "Buckets" To bTemp1
        If (bTemp1 <> True) Begin
            Showln "Missing Buckets collection."
            Procedure_Return
        End

        Move 0 To iIndex
        While (iIndex < (ComNumChildrenAt(hoXml, "Buckets")))
            Set ComI Of hoXml To iIndex
            Get ComGetChildContent Of hoXml "Buckets|Bucket[i]|Name" To sValue
            Get ComSetString Of hoStringCompare sValue To iSuccess
            Get ComContentsEqual Of hoStringCompare "" True To iStringMatch10
            Get ComLastMethodSuccess Of hoXml To bTemp1
            If ((bTemp1 <> True) Or (iStringMatch10 = True)) Begin
                Showln "Missing or empty Buckets|Bucket[i]|Name."
                Procedure_Return
            End

            Showln "Bucket: " sValue
            Move (iIndex + 1) To iIndex
        Loop

        Move "" To sContinuation
        Get ComHasChildWithTag Of hoXml "ContinuationToken" To bTemp1
        If (bTemp1 = True) Begin
            Get ComGetChildContent Of hoXml "ContinuationToken" To sContinuation
            Get ComLastMethodSuccess Of hoXml To bTemp1
            If (bTemp1 <> True) Begin
                Showln "Invalid continuation token."
                Procedure_Return
            End

        End

        Move False To iMore
        Get ComSetString Of hoStringCompare sContinuation To iSuccess
        Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
        If (iStringMatch11 <> True) Begin
            Get ComContains Of hoSeenTokens sContinuation To bTemp1
            If (bTemp1 = True) Begin
                Showln "Repeated continuation token; enumeration is incomplete."
                Procedure_Return
            End

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

            Move True To iMore
        End

    Loop

    Showln "Bucket enumeration complete."


End_Procedure