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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Accept the private XML handoff from either GetSessionToken or AssumeRole.
set xmlToken = Server.CreateObject("Chilkat.Xml")
xmlToken.Strict = 1
success = xmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the private STS credential file.") & "</pre>"
    Response.End
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
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a GetSessionToken or AssumeRole credential response.") & "</pre>"
    Response.End
End If

set path = Server.CreateObject("Chilkat.StringBuilder")
success = path.SetString(credentialPath)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

success = path.Append("|AccessKeyId")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

accessKey = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(accessKey)
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch4 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AccessKeyId.") & "</pre>"
    Response.End
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

success = path.Append("|SecretAccessKey")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

secretKey = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(secretKey)
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch5 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty SecretAccessKey.") & "</pre>"
    Response.End
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

success = path.Append("|SessionToken")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

sessionToken = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(sessionToken)
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch6 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty SessionToken.") & "</pre>"
    Response.End
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

success = path.Append("|Expiration")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build credential path.") & "</pre>"
    Response.End
End If

expiration = xmlToken.GetChildContent(path.GetAsString())
success = stringCompare.SetString(expiration)
stringMatch7 = stringCompare.ContentsEqual("",1)
If ((xmlToken.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty Expiration.") & "</pre>"
    Response.End
End If

set expires = Server.CreateObject("Chilkat.CkDateTime")
set now = Server.CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(expiration)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Stored expiration is invalid.") & "</pre>"
    Response.End
End If

success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
    Response.End
End If

If (expires.DiffSeconds(now) <= 60) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.") & "</pre>"
    Response.End
End If

'  This S3 region is independent of the region that issued the STS credentials.
region = "us-west-2"
set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("s3.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = Server.CreateObject("Chilkat.AuthAws")
aws.AccessKey = accessKey
aws.SecretKey = secretKey
aws.Region = region
aws.ServiceName = "s3"
aws.SignatureVersion = 4
set rest = Server.CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not configure request signing.") & "</pre>"
    Response.End
End If

'  Keep this verified Socket alive while Rest uses its connection.
set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "TLS connection failed; inspect diagnostics privately.") & "</pre>"
    Response.End
End If

success = rest.UseConnection(connection,1)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not attach TLS connection.") & "</pre>"
    Response.End
End If

set xml = Server.CreateObject("Chilkat.Xml")
xml.Strict = 1

set seenTokens = Server.CreateObject("Chilkat.Hashtable")
continuation = ""

page = 0

more = 1
Do While (more = 1)
    If (page = 10000) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Page bound reached; bucket enumeration is incomplete.") & "</pre>"
        Response.End
    End If

    success = now.SetFromCurrentSystemTime()
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
        Response.End
    End If

    If (expires.DiffSeconds(now) <= 60) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Credentials are expiring; obtain fresh credentials before continuing the listing.") & "</pre>"
        Response.End
    End If

    page = page + 1
    success = rest.ClearAllQueryParams()
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not clear parameters from the previous page.") & "</pre>"
        Response.End
    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
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set X-Amz-Security-Token.") & "</pre>"
        Response.End
    End If

    success = rest.AddQueryParam("max-buckets","100")
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set max-buckets.") & "</pre>"
        Response.End
    End If

    success = stringCompare.SetString(continuation)
    stringMatch8 = stringCompare.ContentsEqual("",1)
    If (stringMatch8 <> 1) Then
        success = rest.AddQueryParam("continuation-token",continuation)
        If (success <> 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Could not set continuation-token.") & "</pre>"
            Response.End
        End If

    End If

    responseXml = rest.FullRequestNoBody("GET","/")
    If (rest.LastMethodSuccess <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "S3 request did not complete; enumeration is incomplete.") & "</pre>"
        Response.End
    End If

    success = xml.LoadXml(responseXml)
    If (rest.ResponseStatusCode <> 200) Then
        Response.Write "<pre>" & Server.HTMLEncode( "S3 HTTP status: " & rest.ResponseStatusCode) & "</pre>"
        If (success = 1) Then
            value = xml.GetChildContent("Code")
            If (xml.LastMethodSuccess = 1) Then
                Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & value) & "</pre>"
            End If

            value = xml.GetChildContent("RequestId")
            If (xml.LastMethodSuccess = 1) Then
                Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & value) & "</pre>"
            End If

        End If

        Response.End
    End If

    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid S3 XML response.") & "</pre>"
        Response.End
    End If

    success = stringCompare.SetString(xml.Tag)
    stringMatch9 = stringCompare.ContentsEqual("ListAllMyBucketsResult",1)
    If (stringMatch9 <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Unexpected S3 response root.") & "</pre>"
        Response.End
    End If

    If (xml.HasChildWithTag("Buckets") <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing Buckets collection.") & "</pre>"
        Response.End
    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
            Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty Buckets|Bucket[i]|Name.") & "</pre>"
            Response.End
        End If

        Response.Write "<pre>" & Server.HTMLEncode( "Bucket: " & value) & "</pre>"
        index = index + 1
    Loop
    continuation = ""
    If (xml.HasChildWithTag("ContinuationToken") = 1) Then
        continuation = xml.GetChildContent("ContinuationToken")
        If (xml.LastMethodSuccess <> 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Invalid continuation token.") & "</pre>"
            Response.End
        End If

    End If

    more = 0
    success = stringCompare.SetString(continuation)
    stringMatch11 = stringCompare.ContentsEqual("",1)
    If (stringMatch11 <> 1) Then
        If (seenTokens.Contains(continuation) = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Repeated continuation token; enumeration is incomplete.") & "</pre>"
            Response.End
        End If

        success = seenTokens.AddStr(continuation,"seen")
        If (success <> 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Could not track pagination.") & "</pre>"
            Response.End
        End If

        more = 1
    End If

Loop
Response.Write "<pre>" & Server.HTMLEncode( "Bucket enumeration complete.") & "</pre>"

%>
</body>
</html>