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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoTokenXml
String sCredentialPath
Boolean iStringMatch1
Boolean iStringMatch2
Boolean iStringMatch3
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
String sAccessKey
String sSecretKey
String sSessionToken
String sExpiration
Handle hoPath
Boolean iStringMatch9
Boolean iStringMatch10
Boolean iStringMatch11
Boolean iStringMatch12
Handle hoExpires
Variant vNow
Handle hoNow
Handle hoSession
Handle hoDownload
Boolean iStringMatch13
Boolean iStringMatch14
Handle hoHttp
Variant vObjectData
Handle hoObjectData
String sTemp1
String sTemp2
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// Load a privately stored STS XML response; never print its contents.
Get Create (RefClass(cComChilkatXml)) To hoTokenXml
If (Not(IsComObjectCreated(hoTokenXml))) Begin
Send CreateComObject of hoTokenXml
End
Set ComStrict Of hoTokenXml To True
Get ComLoadXmlFile Of hoTokenXml "qa_data/tokens/aws_session_token.xml" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private STS XML."
Procedure_Return
End
Move "" To sCredentialPath
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetSessionTokenResponse" True To iStringMatch1
If (iStringMatch1 = True) Begin
Move "GetSessionTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleResponse" True To iStringMatch2
If (iStringMatch2 = True) Begin
Move "AssumeRoleResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleWithWebIdentityResponse" True To iStringMatch3
If (iStringMatch3 = True) Begin
Move "AssumeRoleWithWebIdentityResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleWithSAMLResponse" True To iStringMatch4
If (iStringMatch4 = True) Begin
Move "AssumeRoleWithSAMLResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetFederationTokenResponse" True To iStringMatch5
If (iStringMatch5 = True) Begin
Move "GetFederationTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetDelegatedAccessTokenResponse" True To iStringMatch6
If (iStringMatch6 = True) Begin
Move "GetDelegatedAccessTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRootResponse" True To iStringMatch7
If (iStringMatch7 = True) Begin
Move "AssumeRootResult|Credentials" To sCredentialPath
End
Get ComSetString Of hoStringCompare sCredentialPath To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch8
If (iStringMatch8 = True) Begin
Showln "Unsupported STS credential response."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoPath
If (Not(IsComObjectCreated(hoPath))) Begin
Send CreateComObject of hoPath
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|AccessKeyId" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sAccessKey
Get ComSetString Of hoStringCompare sAccessKey To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch9 = True)) Begin
Showln "Missing credential field AccessKeyId."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|SecretAccessKey" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sSecretKey
Get ComSetString Of hoStringCompare sSecretKey To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch10
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch10 = True)) Begin
Showln "Missing credential field SecretAccessKey."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|SessionToken" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sSessionToken
Get ComSetString Of hoStringCompare sSessionToken To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch11 = True)) Begin
Showln "Missing credential field SessionToken."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|Expiration" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sExpiration
Get ComSetString Of hoStringCompare sExpiration To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch12
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch12 = True)) Begin
Showln "Missing credential field Expiration."
Procedure_Return
End
Get Create (RefClass(cComCkDateTime)) To hoExpires
If (Not(IsComObjectCreated(hoExpires))) Begin
Send CreateComObject of hoExpires
End
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromTimestamp Of hoExpires sExpiration To iSuccess
If (iSuccess <> True) Begin
Showln "Stored expiration is invalid."
Procedure_Return
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read current time."
Procedure_Return
End
Get ComDiffSeconds Of hoExpires Get pvComObject of hoNow to vNow
vNow To iTemp1
If (iTemp1 <= 60) Begin
Showln "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoSession
If (Not(IsComObjectCreated(hoSession))) Begin
Send CreateComObject of hoSession
End
Get ComUpdateString Of hoSession "access_key" sAccessKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "secret_key" sSecretKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "session_token" sSessionToken To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "expiration" sExpiration To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoDownload
If (Not(IsComObjectCreated(hoDownload))) Begin
Send CreateComObject of hoDownload
End
Set ComStrict Of hoDownload To True
Get ComLoadFile Of hoDownload "qa_data/aws/sts_download.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load download configuration."
Procedure_Return
End
Get ComStringOf Of hoDownload "bucket" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch13
Get ComJsonTypeOf Of hoDownload "bucket" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch13 = True)) Begin
Showln "Missing bucket."
Procedure_Return
End
Get ComStringOf Of hoDownload "object_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch14
Get ComJsonTypeOf Of hoDownload "object_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch14 = True)) Begin
Showln "Missing object_key."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
Set ComConnectTimeout Of hoHttp To 20
Set ComReadTimeout Of hoHttp To 60
Set ComVerboseLogging Of hoHttp To False
Get ComStringOf Of hoSession "access_key" To sTemp1
Set ComAwsAccessKey Of hoHttp To sTemp1
Get ComStringOf Of hoSession "secret_key" To sTemp1
Set ComAwsSecretKey Of hoHttp To sTemp1
Get ComStringOf Of hoSession "session_token" To sTemp1
Set ComAwsSessionToken Of hoHttp To sTemp1
Set ComAwsRegion Of hoHttp To "us-west-2"
Set ComAwsEndpoint Of hoHttp To "s3.us-west-2.amazonaws.com"
// Current Chilkat S3 methods use Signature Version 4.
Get Create (RefClass(cComChilkatBinData)) To hoObjectData
If (Not(IsComObjectCreated(hoObjectData))) Begin
Send CreateComObject of hoObjectData
End
Get ComStringOf Of hoDownload "bucket" To sTemp1
Get ComStringOf Of hoDownload "object_key" To sTemp2
Get pvComObject of hoObjectData to vObjectData
Get ComS3_DownloadBd Of hoHttp sTemp1 sTemp2 vObjectData To iSuccess
If (iSuccess <> True) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "S3 download failed. HTTP status: " iTemp1
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
If (iTemp1 <> 200) Begin
Showln "Unexpected S3 status; do not use the data."
Procedure_Return
End
Get ComNumBytes Of hoObjectData To iTemp1
Showln "Downloaded bytes: " iTemp1
// Consume objectData in memory; no object contents or credentials are logged.
Showln "Example completed."
End_Procedure