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.
AWS references: AssumeRole
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loTokenXml
LOCAL lcCredentialPath
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL lnStringMatch3
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL loPath
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL lnStringMatch12
LOCAL loExpires
LOCAL loNow
LOCAL loSession
LOCAL loDownload
LOCAL lnStringMatch13
LOCAL lnStringMatch14
LOCAL loHttp
LOCAL loObjectData
lnSuccess = 0
loStringCompare = CreateObject('Chilkat.StringBuilder')
* Load a privately stored STS XML response; never print its contents.
loTokenXml = CreateObject('Chilkat.Xml')
loTokenXml.Strict = 1
lnSuccess = loTokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
IF (lnSuccess <> 1) THEN
? "Could not load private STS XML."
RELEASE loStringCompare
RELEASE loTokenXml
CANCEL
ENDIF
lcCredentialPath = ""
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch1 = loStringCompare.ContentsEqual("GetSessionTokenResponse",1)
IF (lnStringMatch1 = 1) THEN
lcCredentialPath = "GetSessionTokenResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch2 = loStringCompare.ContentsEqual("AssumeRoleResponse",1)
IF (lnStringMatch2 = 1) THEN
lcCredentialPath = "AssumeRoleResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch3 = loStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
IF (lnStringMatch3 = 1) THEN
lcCredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
IF (lnStringMatch4 = 1) THEN
lcCredentialPath = "AssumeRoleWithSAMLResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch5 = loStringCompare.ContentsEqual("GetFederationTokenResponse",1)
IF (lnStringMatch5 = 1) THEN
lcCredentialPath = "GetFederationTokenResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch6 = loStringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
IF (lnStringMatch6 = 1) THEN
lcCredentialPath = "GetDelegatedAccessTokenResult|Credentials"
ENDIF
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch7 = loStringCompare.ContentsEqual("AssumeRootResponse",1)
IF (lnStringMatch7 = 1) THEN
lcCredentialPath = "AssumeRootResult|Credentials"
ENDIF
loStringCompare.SetString(lcCredentialPath)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF (lnStringMatch8 = 1) THEN
? "Unsupported STS credential response."
RELEASE loStringCompare
RELEASE loTokenXml
CANCEL
ENDIF
loPath = CreateObject('Chilkat.StringBuilder')
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|AccessKeyId")
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lcAccessKey = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcAccessKey)
lnStringMatch9 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
? "Missing credential field AccessKeyId."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|SecretAccessKey")
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lcSecretKey = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSecretKey)
lnStringMatch10 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch10 = 1)) THEN
? "Missing credential field SecretAccessKey."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|SessionToken")
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lcSessionToken = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSessionToken)
lnStringMatch11 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch11 = 1)) THEN
? "Missing credential field SessionToken."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lnSuccess = loPath.Append("|Expiration")
IF (lnSuccess <> 1) THEN
? "Could not construct credential path."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
CANCEL
ENDIF
lcExpiration = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcExpiration)
lnStringMatch12 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch12 = 1)) THEN
? "Missing credential field Expiration."
RELEASE loStringCompare
RELEASE loTokenXml
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 loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not read current time."
RELEASE loStringCompare
RELEASE loTokenXml
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 loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
loSession = CreateObject('Chilkat.JsonObject')
lnSuccess = loSession.UpdateString("access_key",lcAccessKey)
IF (lnSuccess <> 1) THEN
? "Could not build normalized session."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
CANCEL
ENDIF
lnSuccess = loSession.UpdateString("secret_key",lcSecretKey)
IF (lnSuccess <> 1) THEN
? "Could not build normalized session."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
CANCEL
ENDIF
lnSuccess = loSession.UpdateString("session_token",lcSessionToken)
IF (lnSuccess <> 1) THEN
? "Could not build normalized session."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
CANCEL
ENDIF
lnSuccess = loSession.UpdateString("expiration",lcExpiration)
IF (lnSuccess <> 1) THEN
? "Could not build normalized session."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
CANCEL
ENDIF
loDownload = CreateObject('Chilkat.JsonObject')
loDownload.Strict = 1
lnSuccess = loDownload.LoadFile("qa_data/aws/sts_download.json")
IF (lnSuccess <> 1) THEN
? "Could not load download configuration."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
CANCEL
ENDIF
loStringCompare.SetString(loDownload.StringOf("bucket"))
lnStringMatch13 = loStringCompare.ContentsEqual("",1)
IF ((loDownload.JsonTypeOf("bucket") <> 1) OR (lnStringMatch13 = 1)) THEN
? "Missing bucket."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
CANCEL
ENDIF
loStringCompare.SetString(loDownload.StringOf("object_key"))
lnStringMatch14 = loStringCompare.ContentsEqual("",1)
IF ((loDownload.JsonTypeOf("object_key") <> 1) OR (lnStringMatch14 = 1)) THEN
? "Missing object_key."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 60
loHttp.VerboseLogging = 0
loHttp.AwsAccessKey = loSession.StringOf("access_key")
loHttp.AwsSecretKey = loSession.StringOf("secret_key")
loHttp.AwsSessionToken = loSession.StringOf("session_token")
loHttp.AwsRegion = "us-west-2"
loHttp.AwsEndpoint = "s3.us-west-2.amazonaws.com"
* Current Chilkat S3 methods use Signature Version 4.
loObjectData = CreateObject('Chilkat.BinData')
lnSuccess = loHttp.S3_DownloadBd(loDownload.StringOf("bucket"),loDownload.StringOf("object_key"),loObjectData)
IF (lnSuccess <> 1) THEN
? "S3 download failed. HTTP status: " + STR(loHttp.LastStatus)
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
RELEASE loHttp
RELEASE loObjectData
CANCEL
ENDIF
IF (loHttp.LastStatus <> 200) THEN
? "Unexpected S3 status; do not use the data."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
RELEASE loHttp
RELEASE loObjectData
CANCEL
ENDIF
? "Downloaded bytes: " + STR(loObjectData.NumBytes)
* Consume objectData in memory; no object contents or credentials are logged.
? "Example completed."
RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loDownload
RELEASE loHttp
RELEASE loObjectData