Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_XmlToken
string ls_CredentialPath
long li_StringMatch1
long li_StringMatch2
long li_StringMatch3
string ls_AccessKey
string ls_SecretKey
string ls_SessionToken
string ls_Expiration
oleobject loo_Path
long li_StringMatch4
long li_StringMatch5
long li_StringMatch6
long li_StringMatch7
oleobject loo_Expires
oleobject loo_Now
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
string ls_Value
oleobject loo_SeenTokens
string ls_Continuation
string ls_ResponseXml
long li_Page
long li_Index
long li_More
long li_StringMatch8
long li_StringMatch9
long li_StringMatch10
long li_StringMatch11

li_Success = 0

loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_StringCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Accept the private XML handoff from either GetSessionToken or AssumeRole.
loo_XmlToken = create oleobject
li_rc = loo_XmlToken.ConnectToNewObject("Chilkat.Xml")

loo_XmlToken.Strict = 1
li_Success = loo_XmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
if li_Success <> 1 then
    Write-Debug "Could not load the private STS credential file."
    destroy loo_StringCompare
    destroy loo_XmlToken
    return
end if

ls_CredentialPath = ""
loo_StringCompare.SetString(loo_XmlToken.Tag)
li_StringMatch1 = loo_StringCompare.ContentsEqual("GetSessionTokenResponse",1)
if li_StringMatch1 = 1 then
    ls_CredentialPath = "GetSessionTokenResult|Credentials"
end if

loo_StringCompare.SetString(loo_XmlToken.Tag)
li_StringMatch2 = loo_StringCompare.ContentsEqual("AssumeRoleResponse",1)
if li_StringMatch2 = 1 then
    ls_CredentialPath = "AssumeRoleResult|Credentials"
end if

loo_StringCompare.SetString(ls_CredentialPath)
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch3 = 1 then
    Write-Debug "Expected a GetSessionToken or AssumeRole credential response."
    destroy loo_StringCompare
    destroy loo_XmlToken
    return
end if

loo_Path = create oleobject
li_rc = loo_Path.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|AccessKeyId")
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

ls_AccessKey = loo_XmlToken.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_AccessKey)
li_StringMatch4 = loo_StringCompare.ContentsEqual("",1)
if (loo_XmlToken.LastMethodSuccess <> 1) OR (li_StringMatch4 = 1) then
    Write-Debug "Missing or empty AccessKeyId."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|SecretAccessKey")
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

ls_SecretKey = loo_XmlToken.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SecretKey)
li_StringMatch5 = loo_StringCompare.ContentsEqual("",1)
if (loo_XmlToken.LastMethodSuccess <> 1) OR (li_StringMatch5 = 1) then
    Write-Debug "Missing or empty SecretAccessKey."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|SessionToken")
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

ls_SessionToken = loo_XmlToken.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SessionToken)
li_StringMatch6 = loo_StringCompare.ContentsEqual("",1)
if (loo_XmlToken.LastMethodSuccess <> 1) OR (li_StringMatch6 = 1) then
    Write-Debug "Missing or empty SessionToken."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|Expiration")
if li_Success <> 1 then
    Write-Debug "Could not build credential path."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

ls_Expiration = loo_XmlToken.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_Expiration)
li_StringMatch7 = loo_StringCompare.ContentsEqual("",1)
if (loo_XmlToken.LastMethodSuccess <> 1) OR (li_StringMatch7 = 1) then
    Write-Debug "Missing or empty Expiration."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    return
end if

loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Expires.SetFromTimestamp(ls_Expiration)
if li_Success <> 1 then
    Write-Debug "Stored expiration is invalid."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not read current time."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

if loo_Expires.DiffSeconds(loo_Now) <= 60 then
    Write-Debug "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

//  This S3 region is independent of the region that issued the STS credentials.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = ls_AccessKey
loo_Aws.SecretKey = ls_SecretKey
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "s3"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_Rest.VerboseLogging = 0
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure request signing."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    return
end if

//  Keep this verified Socket alive while Rest uses its connection.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach TLS connection."
    destroy loo_StringCompare
    destroy loo_XmlToken
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Strict = 1

loo_SeenTokens = create oleobject
li_rc = loo_SeenTokens.ConnectToNewObject("Chilkat.Hashtable")

ls_Continuation = ""

li_Page = 0

li_More = 1
do while (li_More = 1)
    if li_Page = 10000 then
        Write-Debug "Page bound reached; bucket enumeration is incomplete."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    li_Success = loo_Now.SetFromCurrentSystemTime()
    if li_Success <> 1 then
        Write-Debug "Could not read current time."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    if loo_Expires.DiffSeconds(loo_Now) <= 60 then
        Write-Debug "Credentials are expiring; obtain fresh credentials before continuing the listing."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    li_Page = li_Page + 1
    li_Success = loo_Rest.ClearAllQueryParams()
    if li_Success <> 1 then
        Write-Debug "Could not clear parameters from the previous page."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    //  All three credential components are required. Never log this signed URL.
    li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",ls_SessionToken)
    if li_Success <> 1 then
        Write-Debug "Could not set X-Amz-Security-Token."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    li_Success = loo_Rest.AddQueryParam("max-buckets","100")
    if li_Success <> 1 then
        Write-Debug "Could not set max-buckets."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    loo_StringCompare.SetString(ls_Continuation)
    li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
    if li_StringMatch8 <> 1 then
        li_Success = loo_Rest.AddQueryParam("continuation-token",ls_Continuation)
        if li_Success <> 1 then
            Write-Debug "Could not set continuation-token."
            destroy loo_StringCompare
            destroy loo_XmlToken
            destroy loo_Path
            destroy loo_Expires
            destroy loo_Now
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_SeenTokens
            return
        end if

    end if

    ls_ResponseXml = loo_Rest.FullRequestNoBody("GET","/")
    if loo_Rest.LastMethodSuccess <> 1 then
        Write-Debug "S3 request did not complete; enumeration is incomplete."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    li_Success = loo_Xml.LoadXml(ls_ResponseXml)
    if loo_Rest.ResponseStatusCode <> 200 then
        Write-Debug "S3 HTTP status: " + string(loo_Rest.ResponseStatusCode)
        if li_Success = 1 then
            ls_Value = loo_Xml.GetChildContent("Code")
            if loo_Xml.LastMethodSuccess = 1 then
                Write-Debug "AWS error code: " + ls_Value
            end if

            ls_Value = loo_Xml.GetChildContent("RequestId")
            if loo_Xml.LastMethodSuccess = 1 then
                Write-Debug "AWS request ID: " + ls_Value
            end if

        end if

        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    if li_Success <> 1 then
        Write-Debug "Invalid S3 XML response."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    loo_StringCompare.SetString(loo_Xml.Tag)
    li_StringMatch9 = loo_StringCompare.ContentsEqual("ListAllMyBucketsResult",1)
    if li_StringMatch9 <> 1 then
        Write-Debug "Unexpected S3 response root."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    if loo_Xml.HasChildWithTag("Buckets") <> 1 then
        Write-Debug "Missing Buckets collection."
        destroy loo_StringCompare
        destroy loo_XmlToken
        destroy loo_Path
        destroy loo_Expires
        destroy loo_Now
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_SeenTokens
        return
    end if

    li_Index = 0
    do while (li_Index < loo_Xml.NumChildrenAt("Buckets"))
        loo_Xml.I = li_Index
        ls_Value = loo_Xml.GetChildContent("Buckets|Bucket[i]|Name")
        loo_StringCompare.SetString(ls_Value)
        li_StringMatch10 = loo_StringCompare.ContentsEqual("",1)
        if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch10 = 1) then
            Write-Debug "Missing or empty Buckets|Bucket[i]|Name."
            destroy loo_StringCompare
            destroy loo_XmlToken
            destroy loo_Path
            destroy loo_Expires
            destroy loo_Now
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_SeenTokens
            return
        end if

        Write-Debug "Bucket: " + ls_Value
        li_Index = li_Index + 1
    loop
    ls_Continuation = ""
    if loo_Xml.HasChildWithTag("ContinuationToken") = 1 then
        ls_Continuation = loo_Xml.GetChildContent("ContinuationToken")
        if loo_Xml.LastMethodSuccess <> 1 then
            Write-Debug "Invalid continuation token."
            destroy loo_StringCompare
            destroy loo_XmlToken
            destroy loo_Path
            destroy loo_Expires
            destroy loo_Now
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_SeenTokens
            return
        end if

    end if

    li_More = 0
    loo_StringCompare.SetString(ls_Continuation)
    li_StringMatch11 = loo_StringCompare.ContentsEqual("",1)
    if li_StringMatch11 <> 1 then
        if loo_SeenTokens.Contains(ls_Continuation) = 1 then
            Write-Debug "Repeated continuation token; enumeration is incomplete."
            destroy loo_StringCompare
            destroy loo_XmlToken
            destroy loo_Path
            destroy loo_Expires
            destroy loo_Now
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_SeenTokens
            return
        end if

        li_Success = loo_SeenTokens.AddStr(ls_Continuation,"seen")
        if li_Success <> 1 then
            Write-Debug "Could not track pagination."
            destroy loo_StringCompare
            destroy loo_XmlToken
            destroy loo_Path
            destroy loo_Expires
            destroy loo_Now
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_SeenTokens
            return
        end if

        li_More = 1
    end if

loop
Write-Debug "Bucket enumeration complete."


destroy loo_StringCompare
destroy loo_XmlToken
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens