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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loXmlToken
LOCAL lcCredentialPath
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL lnStringMatch3
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL loPath
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL loSeenTokens
LOCAL lcContinuation
LOCAL lcResponseXml
LOCAL lnPage
LOCAL lnIndex
LOCAL lnMore
LOCAL lnStringMatch8
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL lnStringMatch11
lnSuccess = 0
loStringCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
* Accept the private XML handoff from either GetSessionToken or AssumeRole.
loXmlToken = CreateObject('Chilkat.Xml')
loXmlToken.Strict = 1
lnSuccess = loXmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
IF (lnSuccess <> 1) THEN
? "Could not load the private STS credential file."
RELEASE loStringCompare
RELEASE loXmlToken
CANCEL
ENDIF
lcCredentialPath = ""
loStringCompare.SetString(loXmlToken.Tag)
lnStringMatch1 = loStringCompare.ContentsEqual("GetSessionTokenResponse",1)
IF (lnStringMatch1 = 1) THEN
lcCredentialPath = "GetSessionTokenResult|Credentials"
ENDIF
loStringCompare.SetString(loXmlToken.Tag)
lnStringMatch2 = loStringCompare.ContentsEqual("AssumeRoleResponse",1)
IF (lnStringMatch2 = 1) THEN
lcCredentialPath = "AssumeRoleResult|Credentials"
ENDIF
loStringCompare.SetString(lcCredentialPath)
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF (lnStringMatch3 = 1) THEN
? "Expected a GetSessionToken or AssumeRole credential response."
RELEASE loStringCompare
RELEASE loXmlToken
CANCEL
ENDIF
loPath = CreateObject('Chilkat.StringBuilder')
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|AccessKeyId")
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lcAccessKey = loXmlToken.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcAccessKey)
lnStringMatch4 = loStringCompare.ContentsEqual("",1)
IF ((loXmlToken.LastMethodSuccess <> 1) OR (lnStringMatch4 = 1)) THEN
? "Missing or empty AccessKeyId."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|SecretAccessKey")
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lcSecretKey = loXmlToken.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSecretKey)
lnStringMatch5 = loStringCompare.ContentsEqual("",1)
IF ((loXmlToken.LastMethodSuccess <> 1) OR (lnStringMatch5 = 1)) THEN
? "Missing or empty SecretAccessKey."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|SessionToken")
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lcSessionToken = loXmlToken.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSessionToken)
lnStringMatch6 = loStringCompare.ContentsEqual("",1)
IF ((loXmlToken.LastMethodSuccess <> 1) OR (lnStringMatch6 = 1)) THEN
? "Missing or empty SessionToken."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|Expiration")
IF (lnSuccess <> 1) THEN
? "Could not build credential path."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
lcExpiration = loXmlToken.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcExpiration)
lnStringMatch7 = loStringCompare.ContentsEqual("",1)
IF ((loXmlToken.LastMethodSuccess <> 1) OR (lnStringMatch7 = 1)) THEN
? "Missing or empty Expiration."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
CANCEL
ENDIF
loExpires = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
lnSuccess = loExpires.SetFromTimestamp(lcExpiration)
IF (lnSuccess <> 1) THEN
? "Stored expiration is invalid."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not read current time."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
IF (loExpires.DiffSeconds(loNow) <= 60) THEN
? "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
* This S3 region is independent of the region that issued the STS credentials.
lcRegion = "us-west-2"
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = lcAccessKey
loAws.SecretKey = lcSecretKey
loAws.Region = lcRegion
loAws.ServiceName = "s3"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loRest.VerboseLogging = 0
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
? "Could not configure request signing."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
* Keep this verified Socket alive while Rest uses its connection.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
? "Could not attach TLS connection."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
loSeenTokens = CreateObject('Chilkat.Hashtable')
lcContinuation = ""
lnPage = 0
lnMore = 1
DO WHILE (lnMore = 1)
IF (lnPage = 10000) THEN
? "Page bound reached; bucket enumeration is incomplete."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not read current time."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
IF (loExpires.DiffSeconds(loNow) <= 60) THEN
? "Credentials are expiring; obtain fresh credentials before continuing the listing."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnPage = lnPage + 1
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
? "Could not clear parameters from the previous page."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
* All three credential components are required. Never log this signed URL.
lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",lcSessionToken)
IF (lnSuccess <> 1) THEN
? "Could not set X-Amz-Security-Token."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("max-buckets","100")
IF (lnSuccess <> 1) THEN
? "Could not set max-buckets."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
loStringCompare.SetString(lcContinuation)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF (lnStringMatch8 <> 1) THEN
lnSuccess = loRest.AddQueryParam("continuation-token",lcContinuation)
IF (lnSuccess <> 1) THEN
? "Could not set continuation-token."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
ENDIF
lcResponseXml = loRest.FullRequestNoBody("GET","/")
IF (loRest.LastMethodSuccess <> 1) THEN
? "S3 request did not complete; enumeration is incomplete."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnSuccess = loXml.LoadXml(lcResponseXml)
IF (loRest.ResponseStatusCode <> 200) THEN
? "S3 HTTP status: " + STR(loRest.ResponseStatusCode)
IF (lnSuccess = 1) THEN
lcValue = loXml.GetChildContent("Code")
IF (loXml.LastMethodSuccess = 1) THEN
? "AWS error code: " + lcValue
ENDIF
lcValue = loXml.GetChildContent("RequestId")
IF (loXml.LastMethodSuccess = 1) THEN
? "AWS request ID: " + lcValue
ENDIF
ENDIF
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
IF (lnSuccess <> 1) THEN
? "Invalid S3 XML response."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
loStringCompare.SetString(loXml.Tag)
lnStringMatch9 = loStringCompare.ContentsEqual("ListAllMyBucketsResult",1)
IF (lnStringMatch9 <> 1) THEN
? "Unexpected S3 response root."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
IF (loXml.HasChildWithTag("Buckets") <> 1) THEN
? "Missing Buckets collection."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnIndex = 0
DO WHILE (lnIndex < loXml.NumChildrenAt("Buckets"))
loXml.I = lnIndex
lcValue = loXml.GetChildContent("Buckets|Bucket[i]|Name")
loStringCompare.SetString(lcValue)
lnStringMatch10 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch10 = 1)) THEN
? "Missing or empty Buckets|Bucket[i]|Name."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
? "Bucket: " + lcValue
lnIndex = lnIndex + 1
ENDDO
lcContinuation = ""
IF (loXml.HasChildWithTag("ContinuationToken") = 1) THEN
lcContinuation = loXml.GetChildContent("ContinuationToken")
IF (loXml.LastMethodSuccess <> 1) THEN
? "Invalid continuation token."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
ENDIF
lnMore = 0
loStringCompare.SetString(lcContinuation)
lnStringMatch11 = loStringCompare.ContentsEqual("",1)
IF (lnStringMatch11 <> 1) THEN
IF (loSeenTokens.Contains(lcContinuation) = 1) THEN
? "Repeated continuation token; enumeration is incomplete."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnSuccess = loSeenTokens.AddStr(lcContinuation,"seen")
IF (lnSuccess <> 1) THEN
? "Could not track pagination."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens
CANCEL
ENDIF
lnMore = 1
ENDIF
ENDDO
? "Bucket enumeration complete."
RELEASE loStringCompare
RELEASE loXmlToken
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSeenTokens