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

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
;  Accept the private XML handoff from either GetSessionToken or AssumeRole.
$oXmlToken = ObjCreate("Chilkat.Xml")
$oXmlToken.Strict = True
$bSuccess = $oXmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private STS credential file." & @CRLF)
    Exit
EndIf

Local $sCredentialPath = ""
$oStringCompare.SetString($oXmlToken.Tag)
Local $bStringMatch1 = $oStringCompare.ContentsEqual("GetSessionTokenResponse",True)
If ($bStringMatch1 = True) Then
    $sCredentialPath = "GetSessionTokenResult|Credentials"
EndIf

$oStringCompare.SetString($oXmlToken.Tag)
Local $bStringMatch2 = $oStringCompare.ContentsEqual("AssumeRoleResponse",True)
If ($bStringMatch2 = True) Then
    $sCredentialPath = "AssumeRoleResult|Credentials"
EndIf

$oStringCompare.SetString($sCredentialPath)
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch3 = True) Then
    ConsoleWrite("Expected a GetSessionToken or AssumeRole credential response." & @CRLF)
    Exit
EndIf

Local $sAccessKey
Local $secretKey
Local $sessionToken
Local $sExpiration
$oPath = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|AccessKeyId")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$sAccessKey = $oXmlToken.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sAccessKey)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("",True)
If (($oXmlToken.LastMethodSuccess <> True) Or ($bStringMatch4 = True)) Then
    ConsoleWrite("Missing or empty AccessKeyId." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|SecretAccessKey")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$secretKey = $oXmlToken.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($secretKey)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("",True)
If (($oXmlToken.LastMethodSuccess <> True) Or ($bStringMatch5 = True)) Then
    ConsoleWrite("Missing or empty SecretAccessKey." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|SessionToken")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$sessionToken = $oXmlToken.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sessionToken)
Local $bStringMatch6 = $oStringCompare.ContentsEqual("",True)
If (($oXmlToken.LastMethodSuccess <> True) Or ($bStringMatch6 = True)) Then
    ConsoleWrite("Missing or empty SessionToken." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|Expiration")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build credential path." & @CRLF)
    Exit
EndIf

$sExpiration = $oXmlToken.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sExpiration)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("",True)
If (($oXmlToken.LastMethodSuccess <> True) Or ($bStringMatch7 = True)) Then
    ConsoleWrite("Missing or empty Expiration." & @CRLF)
    Exit
EndIf

$oExpires = ObjCreate("Chilkat.CkDateTime")
$oNow = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oExpires.SetFromTimestamp($sExpiration)
If ($bSuccess <> True) Then
    ConsoleWrite("Stored expiration is invalid." & @CRLF)
    Exit
EndIf

$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read current time." & @CRLF)
    Exit
EndIf

If ($oExpires.DiffSeconds($oNow) <= 60) Then
    ConsoleWrite("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials." & @CRLF)
    Exit
EndIf

;  This S3 region is independent of the region that issued the STS credentials.
Local $sRegion = "us-west-2"
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("s3.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oAws = ObjCreate("Chilkat.AuthAws")
$oAws.AccessKey = $sAccessKey
$oAws.SecretKey = $secretKey
$oAws.Region = $sRegion
$oAws.ServiceName = "s3"
$oAws.SignatureVersion = 4
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$oRest.VerboseLogging = False
$bSuccess = $oRest.SetAuthAws($oAws)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not configure request signing." & @CRLF)
    Exit
EndIf

;  Keep this verified Socket alive while Rest uses its connection.
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
    ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.UseConnection($oConnection,True)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not attach TLS connection." & @CRLF)
    Exit
EndIf

$oXml = ObjCreate("Chilkat.Xml")
$oXml.Strict = True
Local $sValue
$oSeenTokens = ObjCreate("Chilkat.Hashtable")
Local $sContinuation = ""
Local $sResponseXml
Local $iPage = 0
Local $index
Local $bMore = True
While ($bMore = True)
    If ($iPage = 10000) Then
        ConsoleWrite("Page bound reached; bucket enumeration is incomplete." & @CRLF)
        Exit
    EndIf

    $bSuccess = $oNow.SetFromCurrentSystemTime()
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not read current time." & @CRLF)
        Exit
    EndIf

    If ($oExpires.DiffSeconds($oNow) <= 60) Then
        ConsoleWrite("Credentials are expiring; obtain fresh credentials before continuing the listing." & @CRLF)
        Exit
    EndIf

    $iPage = $iPage + 1
    $bSuccess = $oRest.ClearAllQueryParams()
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not clear parameters from the previous page." & @CRLF)
        Exit
    EndIf

    ;  All three credential components are required. Never log this signed URL.
    $bSuccess = $oRest.AddQueryParam("X-Amz-Security-Token",$sessionToken)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not set X-Amz-Security-Token." & @CRLF)
        Exit
    EndIf

    $bSuccess = $oRest.AddQueryParam("max-buckets","100")
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not set max-buckets." & @CRLF)
        Exit
    EndIf

    $oStringCompare.SetString($sContinuation)
Local $bStringMatch8 = $oStringCompare.ContentsEqual("",True)
    If ($bStringMatch8 <> True) Then
        $bSuccess = $oRest.AddQueryParam("continuation-token",$sContinuation)
        If ($bSuccess <> True) Then
            ConsoleWrite("Could not set continuation-token." & @CRLF)
            Exit
        EndIf

    EndIf

    $sResponseXml = $oRest.FullRequestNoBody("GET","/")
    If ($oRest.LastMethodSuccess <> True) Then
        ConsoleWrite("S3 request did not complete; enumeration is incomplete." & @CRLF)
        Exit
    EndIf

    $bSuccess = $oXml.LoadXml($sResponseXml)
    If ($oRest.ResponseStatusCode <> 200) Then
        ConsoleWrite("S3 HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
        If ($bSuccess = True) Then
            $sValue = $oXml.GetChildContent("Code")
            If ($oXml.LastMethodSuccess = True) Then
                ConsoleWrite("AWS error code: " & $sValue & @CRLF)
            EndIf

            $sValue = $oXml.GetChildContent("RequestId")
            If ($oXml.LastMethodSuccess = True) Then
                ConsoleWrite("AWS request ID: " & $sValue & @CRLF)
            EndIf

        EndIf

        Exit
    EndIf

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

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

    If ($oXml.HasChildWithTag("Buckets") <> True) Then
        ConsoleWrite("Missing Buckets collection." & @CRLF)
        Exit
    EndIf

    $index = 0
    While ($index < $oXml.NumChildrenAt("Buckets"))
        $oXml.I = $index
        $sValue = $oXml.GetChildContent("Buckets|Bucket[i]|Name")
        $oStringCompare.SetString($sValue)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("",True)
        If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch10 = True)) Then
            ConsoleWrite("Missing or empty Buckets|Bucket[i]|Name." & @CRLF)
            Exit
        EndIf

        ConsoleWrite("Bucket: " & $sValue & @CRLF)
        $index = $index + 1
    Wend
    $sContinuation = ""
    If ($oXml.HasChildWithTag("ContinuationToken") = True) Then
        $sContinuation = $oXml.GetChildContent("ContinuationToken")
        If ($oXml.LastMethodSuccess <> True) Then
            ConsoleWrite("Invalid continuation token." & @CRLF)
            Exit
        EndIf

    EndIf

    $bMore = False
    $oStringCompare.SetString($sContinuation)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("",True)
    If ($bStringMatch11 <> True) Then
        If ($oSeenTokens.Contains($sContinuation) = True) Then
            ConsoleWrite("Repeated continuation token; enumeration is incomplete." & @CRLF)
            Exit
        EndIf

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

        $bMore = True
    EndIf

Wend
ConsoleWrite("Bucket enumeration complete." & @CRLF)