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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
set stringCompare = CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Accept the private XML handoff from either GetSessionToken or AssumeRole.
set xmlToken = CreateObject("Chilkat.Xml")
xmlToken.Strict = 1
success = xmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If (success <> 1) Then
outFile.WriteLine("Could not load the private STS credential file.")
WScript.Quit
End If
credentialPath = ""
success = stringCompare.SetString(xmlToken.Tag)
stringMatch1 = stringCompare.ContentsEqual("GetSessionTokenResponse",1)
If (stringMatch1 = 1) Then
credentialPath = "GetSessionTokenResult|Credentials"
End If
success = stringCompare.SetString(xmlToken.Tag)
stringMatch2 = stringCompare.ContentsEqual("AssumeRoleResponse",1)
If (stringMatch2 = 1) Then
credentialPath = "AssumeRoleResult|Credentials"
End If
success = stringCompare.SetString(credentialPath)
stringMatch3 = stringCompare.ContentsEqual("",1)
If (stringMatch3 = 1) Then
outFile.WriteLine("Expected a GetSessionToken or AssumeRole credential response.")
WScript.Quit
End If
set path = CreateObject("Chilkat.StringBuilder")
success = path.SetString(credentialPath)
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
success = path.Append("|AccessKeyId")
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
accessKey = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(accessKey)
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch4 = 1)) Then
outFile.WriteLine("Missing or empty AccessKeyId.")
WScript.Quit
End If
success = path.SetString(credentialPath)
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
success = path.Append("|SecretAccessKey")
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
secretKey = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(secretKey)
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch5 = 1)) Then
outFile.WriteLine("Missing or empty SecretAccessKey.")
WScript.Quit
End If
success = path.SetString(credentialPath)
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
success = path.Append("|SessionToken")
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
sessionToken = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(sessionToken)
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch6 = 1)) Then
outFile.WriteLine("Missing or empty SessionToken.")
WScript.Quit
End If
success = path.SetString(credentialPath)
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
success = path.Append("|Expiration")
If (success <> 1) Then
outFile.WriteLine("Could not build credential path.")
WScript.Quit
End If
expiration = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(expiration)
stringMatch7 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
outFile.WriteLine("Missing or empty Expiration.")
WScript.Quit
End If
set expires = CreateObject("Chilkat.CkDateTime")
set now = CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(expiration)
If (success <> 1) Then
outFile.WriteLine("Stored expiration is invalid.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.")
WScript.Quit
End If
' This S3 region is independent of the region that issued the STS credentials.
region = "us-west-2"
set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("s3.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = CreateObject("Chilkat.AuthAws")
aws.AccessKey = accessKey
aws.SecretKey = secretKey
aws.Region = region
aws.ServiceName = "s3"
aws.SignatureVersion = 4
set rest = CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
outFile.WriteLine("Could not configure request signing.")
WScript.Quit
End If
' Keep this verified Socket alive while Rest uses its connection.
set connection = CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
outFile.WriteLine("TLS connection failed; inspect diagnostics privately.")
WScript.Quit
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
outFile.WriteLine("Could not attach TLS connection.")
WScript.Quit
End If
set xml = CreateObject("Chilkat.Xml")
xml.Strict = 1
set seenTokens = CreateObject("Chilkat.Hashtable")
continuation = ""
page = 0
more = 1
Do While (more = 1)
If (page = 10000) Then
outFile.WriteLine("Page bound reached; bucket enumeration is incomplete.")
WScript.Quit
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read current time.")
WScript.Quit
End If
If (expires.DiffSeconds(now) <= 60) Then
outFile.WriteLine("Credentials are expiring; obtain fresh credentials before continuing the listing.")
WScript.Quit
End If
page = page + 1
success = rest.ClearAllQueryParams()
If (success <> 1) Then
outFile.WriteLine("Could not clear parameters from the previous page.")
WScript.Quit
End If
' All three credential components are required. Never log this signed URL.
success = rest.AddQueryParam("X-Amz-Security-Token",sessionToken)
If (success <> 1) Then
outFile.WriteLine("Could not set X-Amz-Security-Token.")
WScript.Quit
End If
success = rest.AddQueryParam("max-buckets","100")
If (success <> 1) Then
outFile.WriteLine("Could not set max-buckets.")
WScript.Quit
End If
success = stringCompare.SetString(continuation)
stringMatch8 = stringCompare.ContentsEqual("",1)
If (stringMatch8 <> 1) Then
success = rest.AddQueryParam("continuation-token",continuation)
If (success <> 1) Then
outFile.WriteLine("Could not set continuation-token.")
WScript.Quit
End If
End If
responseXml = rest.FullRequestNoBody("GET","/")
If (rest.LastMethodSuccess <> 1) Then
outFile.WriteLine("S3 request did not complete; enumeration is incomplete.")
WScript.Quit
End If
success = xml.LoadXml(responseXml)
If (rest.ResponseStatusCode <> 200) Then
outFile.WriteLine("S3 HTTP status: " & rest.ResponseStatusCode)
If (success = 1) Then
value = xml.GetChildContent("Code")
If (xml.LastMethodSuccess = 1) Then
outFile.WriteLine("AWS error code: " & value)
End If
value = xml.GetChildContent("RequestId")
If (xml.LastMethodSuccess = 1) Then
outFile.WriteLine("AWS request ID: " & value)
End If
End If
WScript.Quit
End If
If (success <> 1) Then
outFile.WriteLine("Invalid S3 XML response.")
WScript.Quit
End If
success = stringCompare.SetString(xml.Tag)
stringMatch9 = stringCompare.ContentsEqual("ListAllMyBucketsResult",1)
If (stringMatch9 <> 1) Then
outFile.WriteLine("Unexpected S3 response root.")
WScript.Quit
End If
If (xml.HasChildWithTag("Buckets") <> 1) Then
outFile.WriteLine("Missing Buckets collection.")
WScript.Quit
End If
index = 0
Do While (index < xml.NumChildrenAt("Buckets"))
xml.I = index
value = xml.GetChildContent("Buckets|Bucket[i]|Name")
success = stringCompare.SetString(value)
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch10 = 1)) Then
outFile.WriteLine("Missing or empty Buckets|Bucket[i]|Name.")
WScript.Quit
End If
outFile.WriteLine("Bucket: " & value)
index = index + 1
Loop
continuation = ""
If (xml.HasChildWithTag("ContinuationToken") = 1) Then
continuation = xml.GetChildContent("ContinuationToken")
If (xml.LastMethodSuccess <> 1) Then
outFile.WriteLine("Invalid continuation token.")
WScript.Quit
End If
End If
more = 0
success = stringCompare.SetString(continuation)
stringMatch11 = stringCompare.ContentsEqual("",1)
If (stringMatch11 <> 1) Then
If (seenTokens.Contains(continuation) = 1) Then
outFile.WriteLine("Repeated continuation token; enumeration is incomplete.")
WScript.Quit
End If
success = seenTokens.AddStr(continuation,"seen")
If (success <> 1) Then
outFile.WriteLine("Could not track pagination.")
WScript.Quit
End If
more = 1
End If
Loop
outFile.WriteLine("Bucket enumeration complete.")
outFile.Close