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

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  Load a privately stored STS XML response; never print its contents.
$oTokenXml = ObjCreate("Chilkat.Xml")
$oTokenXml.Strict = True
$bSuccess = $oTokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private STS XML." & @CRLF)
    Exit
EndIf

Local $sCredentialPath = ""
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch1 = $oStringCompare.ContentsEqual("GetSessionTokenResponse",True)
If ($bStringMatch1 = True) Then
    $sCredentialPath = "GetSessionTokenResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch2 = $oStringCompare.ContentsEqual("AssumeRoleResponse",True)
If ($bStringMatch2 = True) Then
    $sCredentialPath = "AssumeRoleResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch3 = $oStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",True)
If ($bStringMatch3 = True) Then
    $sCredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",True)
If ($bStringMatch4 = True) Then
    $sCredentialPath = "AssumeRoleWithSAMLResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("GetFederationTokenResponse",True)
If ($bStringMatch5 = True) Then
    $sCredentialPath = "GetFederationTokenResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch6 = $oStringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",True)
If ($bStringMatch6 = True) Then
    $sCredentialPath = "GetDelegatedAccessTokenResult|Credentials"
EndIf

$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("AssumeRootResponse",True)
If ($bStringMatch7 = True) Then
    $sCredentialPath = "AssumeRootResult|Credentials"
EndIf

$oStringCompare.SetString($sCredentialPath)
Local $bStringMatch8 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch8 = True) Then
    ConsoleWrite("Unsupported STS credential response." & @CRLF)
    Exit
EndIf

Local $sAccessKey
Local $secretKey
Local $sessionToken
Local $sExpiration
$oPath = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|AccessKeyId")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$sAccessKey = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sAccessKey)
Local $bStringMatch9 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch9 = True)) Then
    ConsoleWrite("Missing credential field AccessKeyId." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|SecretAccessKey")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$secretKey = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($secretKey)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch10 = True)) Then
    ConsoleWrite("Missing credential field SecretAccessKey." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|SessionToken")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$sessionToken = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sessionToken)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch11 = True)) Then
    ConsoleWrite("Missing credential field SessionToken." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$bSuccess = $oPath.Append("|Expiration")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not construct credential path." & @CRLF)
    Exit
EndIf

$sExpiration = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sExpiration)
Local $bStringMatch12 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch12 = True)) Then
    ConsoleWrite("Missing credential field Expiration." & @CRLF)
    Exit
EndIf

$oExpires = ObjCreate("Chilkat.CkDateTime")
$oNow = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oExpires.SetFromTimestamp($sExpiration)
If ($bSuccess <> True) Then
    ConsoleWrite("Stored expiration is invalid." & @CRLF)
    Exit
EndIf

$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read current time." & @CRLF)
    Exit
EndIf

If ($oExpires.DiffSeconds($oNow) <= 60) Then
    ConsoleWrite("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials." & @CRLF)
    Exit
EndIf

$oSession = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oSession.UpdateString("access_key",$sAccessKey)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build normalized session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("secret_key",$secretKey)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build normalized session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("session_token",$sessionToken)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build normalized session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("expiration",$sExpiration)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build normalized session." & @CRLF)
    Exit
EndIf

$oDownload = ObjCreate("Chilkat.JsonObject")
$oDownload.Strict = True
$bSuccess = $oDownload.LoadFile("qa_data/aws/sts_download.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load download configuration." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oDownload.StringOf("bucket"))
Local $bStringMatch13 = $oStringCompare.ContentsEqual("",True)
If (($oDownload.JsonTypeOf("bucket") <> 1) Or ($bStringMatch13 = True)) Then
    ConsoleWrite("Missing bucket." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oDownload.StringOf("object_key"))
Local $bStringMatch14 = $oStringCompare.ContentsEqual("",True)
If (($oDownload.JsonTypeOf("object_key") <> 1) Or ($bStringMatch14 = True)) Then
    ConsoleWrite("Missing object_key." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 20
$oHttp.ReadTimeout = 60
$oHttp.VerboseLogging = False
$oHttp.AwsAccessKey = $oSession.StringOf("access_key")
$oHttp.AwsSecretKey = $oSession.StringOf("secret_key")
$oHttp.AwsSessionToken = $oSession.StringOf("session_token")
$oHttp.AwsRegion = "us-west-2"
$oHttp.AwsEndpoint = "s3.us-west-2.amazonaws.com"
;  Current Chilkat S3 methods use Signature Version 4.
$oObjectData = ObjCreate("Chilkat.BinData")
$bSuccess = $oHttp.S3_DownloadBd($oDownload.StringOf("bucket"),$oDownload.StringOf("object_key"),$oObjectData)
If ($bSuccess <> True) Then
    ConsoleWrite("S3 download failed. HTTP status: " & $oHttp.LastStatus & @CRLF)
    Exit
EndIf

If ($oHttp.LastStatus <> 200) Then
    ConsoleWrite("Unexpected S3 status; do not use the data." & @CRLF)
    Exit
EndIf

ConsoleWrite("Downloaded bytes: " & $oObjectData.NumBytes & @CRLF)
;  Consume objectData in memory; no object contents or credentials are logged.
ConsoleWrite("Example completed." & @CRLF)