Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.6.0+

AWS STS: Download an S3 Object with STS Credentials and Chilkat.Http

See more AWS Security Token Service Examples

Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.

Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.

Reads private STS XML as in the normalization example, plus qa_data/aws/sts_download.json with bucket and object_key strings. Uses all three temporary credential components with Chilkat.Http.S3_DownloadBd. Supply the raw object key, not a pre-encoded URL. Choose a small object that fits in memory, a general purpose bucket in us-west-2, and a session authorized for s3:GetObject on that object; the earlier bucket-list-only session policy is insufficient. KMS-encrypted objects may also need kms:Decrypt. The request is read-only and writes no file. Redirects are disabled so a region mismatch fails explicitly.

S3 GetObject

AWS references: AssumeRole

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")
'  Load a privately stored STS XML response; never print its contents.
set tokenXml = Server.CreateObject("Chilkat.Xml")
tokenXml.Strict = 1
success = tokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load private STS XML.") & "</pre>"
    Response.End
End If

credentialPath = ""
success = stringCompare.SetString(tokenXml.Tag)
stringMatch1 = stringCompare.ContentsEqual("GetSessionTokenResponse",1)
If (stringMatch1 = 1) Then
    credentialPath = "GetSessionTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch2 = stringCompare.ContentsEqual("AssumeRoleResponse",1)
If (stringMatch2 = 1) Then
    credentialPath = "AssumeRoleResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch3 = stringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
If (stringMatch3 = 1) Then
    credentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch4 = stringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
If (stringMatch4 = 1) Then
    credentialPath = "AssumeRoleWithSAMLResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch5 = stringCompare.ContentsEqual("GetFederationTokenResponse",1)
If (stringMatch5 = 1) Then
    credentialPath = "GetFederationTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch6 = stringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
If (stringMatch6 = 1) Then
    credentialPath = "GetDelegatedAccessTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch7 = stringCompare.ContentsEqual("AssumeRootResponse",1)
If (stringMatch7 = 1) Then
    credentialPath = "AssumeRootResult|Credentials"
End If

success = stringCompare.SetString(credentialPath)
stringMatch8 = stringCompare.ContentsEqual("",1)
If (stringMatch8 = 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unsupported STS 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 construct credential path.") & "</pre>"
    Response.End
End If

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

accessKey = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(accessKey)
stringMatch9 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch9 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing credential field AccessKeyId.") & "</pre>"
    Response.End
End If

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

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

secretKey = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(secretKey)
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch10 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing credential field SecretAccessKey.") & "</pre>"
    Response.End
End If

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

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

sessionToken = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(sessionToken)
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch11 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing credential field SessionToken.") & "</pre>"
    Response.End
End If

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

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

expiration = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(expiration)
stringMatch12 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch12 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing credential field 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

set session = Server.CreateObject("Chilkat.JsonObject")
success = session.UpdateString("access_key",accessKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build normalized session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("secret_key",secretKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build normalized session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("session_token",sessionToken)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build normalized session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("expiration",expiration)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build normalized session.") & "</pre>"
    Response.End
End If

set download = Server.CreateObject("Chilkat.JsonObject")
download.Strict = 1
success = download.LoadFile("qa_data/aws/sts_download.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load download configuration.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(download.StringOf("bucket"))
stringMatch13 = stringCompare.ContentsEqual("",1)
If ((download.JsonTypeOf("bucket") <> 1) Or (stringMatch13 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing bucket.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(download.StringOf("object_key"))
stringMatch14 = stringCompare.ContentsEqual("",1)
If ((download.JsonTypeOf("object_key") <> 1) Or (stringMatch14 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing object_key.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = 0
http.AwsAccessKey = session.StringOf("access_key")
http.AwsSecretKey = session.StringOf("secret_key")
http.AwsSessionToken = session.StringOf("session_token")
http.AwsRegion = "us-west-2"
http.AwsEndpoint = "s3.us-west-2.amazonaws.com"
'  Current Chilkat S3 methods use Signature Version 4.
set objectData = Server.CreateObject("Chilkat.BinData")
success = http.S3_DownloadBd(download.StringOf("bucket"),download.StringOf("object_key"),objectData)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "S3 download failed. HTTP status: " & http.LastStatus) & "</pre>"
    Response.End
End If

If (http.LastStatus <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected S3 status; do not use the data.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Downloaded bytes: " & objectData.NumBytes) & "</pre>"
'  Consume objectData in memory; no object contents or credentials are logged.
Response.Write "<pre>" & Server.HTMLEncode( "Example completed.") & "</pre>"

%>
</body>
</html>