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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_TokenXml
string ls_CredentialPath
long li_StringMatch1
long li_StringMatch2
long li_StringMatch3
long li_StringMatch4
long li_StringMatch5
long li_StringMatch6
long li_StringMatch7
long li_StringMatch8
string ls_AccessKey
string ls_SecretKey
string ls_SessionToken
string ls_Expiration
oleobject loo_Path
long li_StringMatch9
long li_StringMatch10
long li_StringMatch11
long li_StringMatch12
oleobject loo_Expires
oleobject loo_Now
oleobject loo_Session
oleobject loo_Download
long li_StringMatch13
long li_StringMatch14
oleobject loo_Http
oleobject loo_ObjectData
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Load a privately stored STS XML response; never print its contents.
loo_TokenXml = create oleobject
li_rc = loo_TokenXml.ConnectToNewObject("Chilkat.Xml")
loo_TokenXml.Strict = 1
li_Success = loo_TokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
if li_Success <> 1 then
Write-Debug "Could not load private STS XML."
destroy loo_StringCompare
destroy loo_TokenXml
return
end if
ls_CredentialPath = ""
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch1 = loo_StringCompare.ContentsEqual("GetSessionTokenResponse",1)
if li_StringMatch1 = 1 then
ls_CredentialPath = "GetSessionTokenResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch2 = loo_StringCompare.ContentsEqual("AssumeRoleResponse",1)
if li_StringMatch2 = 1 then
ls_CredentialPath = "AssumeRoleResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch3 = loo_StringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
if li_StringMatch3 = 1 then
ls_CredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch4 = loo_StringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
if li_StringMatch4 = 1 then
ls_CredentialPath = "AssumeRoleWithSAMLResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch5 = loo_StringCompare.ContentsEqual("GetFederationTokenResponse",1)
if li_StringMatch5 = 1 then
ls_CredentialPath = "GetFederationTokenResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch6 = loo_StringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
if li_StringMatch6 = 1 then
ls_CredentialPath = "GetDelegatedAccessTokenResult|Credentials"
end if
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch7 = loo_StringCompare.ContentsEqual("AssumeRootResponse",1)
if li_StringMatch7 = 1 then
ls_CredentialPath = "AssumeRootResult|Credentials"
end if
loo_StringCompare.SetString(ls_CredentialPath)
li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch8 = 1 then
Write-Debug "Unsupported STS credential response."
destroy loo_StringCompare
destroy loo_TokenXml
return
end if
loo_Path = create oleobject
li_rc = loo_Path.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.Append("|AccessKeyId")
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
ls_AccessKey = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_AccessKey)
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch9 = 1) then
Write-Debug "Missing credential field AccessKeyId."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.Append("|SecretAccessKey")
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
ls_SecretKey = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SecretKey)
li_StringMatch10 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch10 = 1) then
Write-Debug "Missing credential field SecretAccessKey."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.Append("|SessionToken")
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
ls_SessionToken = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SessionToken)
li_StringMatch11 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch11 = 1) then
Write-Debug "Missing credential field SessionToken."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
li_Success = loo_Path.Append("|Expiration")
if li_Success <> 1 then
Write-Debug "Could not construct credential path."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
ls_Expiration = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_Expiration)
li_StringMatch12 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch12 = 1) then
Write-Debug "Missing credential field Expiration."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
return
end if
loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
li_Success = loo_Expires.SetFromTimestamp(ls_Expiration)
if li_Success <> 1 then
Write-Debug "Stored expiration is invalid."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
return
end if
li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
Write-Debug "Could not read current time."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
return
end if
if loo_Expires.DiffSeconds(loo_Now) <= 60 then
Write-Debug "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
return
end if
loo_Session = create oleobject
li_rc = loo_Session.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Session.UpdateString("access_key",ls_AccessKey)
if li_Success <> 1 then
Write-Debug "Could not build normalized session."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
return
end if
li_Success = loo_Session.UpdateString("secret_key",ls_SecretKey)
if li_Success <> 1 then
Write-Debug "Could not build normalized session."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
return
end if
li_Success = loo_Session.UpdateString("session_token",ls_SessionToken)
if li_Success <> 1 then
Write-Debug "Could not build normalized session."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
return
end if
li_Success = loo_Session.UpdateString("expiration",ls_Expiration)
if li_Success <> 1 then
Write-Debug "Could not build normalized session."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
return
end if
loo_Download = create oleobject
li_rc = loo_Download.ConnectToNewObject("Chilkat.JsonObject")
loo_Download.Strict = 1
li_Success = loo_Download.LoadFile("qa_data/aws/sts_download.json")
if li_Success <> 1 then
Write-Debug "Could not load download configuration."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
return
end if
loo_StringCompare.SetString(loo_Download.StringOf("bucket"))
li_StringMatch13 = loo_StringCompare.ContentsEqual("",1)
if (loo_Download.JsonTypeOf("bucket") <> 1) OR (li_StringMatch13 = 1) then
Write-Debug "Missing bucket."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
return
end if
loo_StringCompare.SetString(loo_Download.StringOf("object_key"))
li_StringMatch14 = loo_StringCompare.ContentsEqual("",1)
if (loo_Download.JsonTypeOf("object_key") <> 1) OR (li_StringMatch14 = 1) then
Write-Debug "Missing object_key."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 60
loo_Http.VerboseLogging = 0
loo_Http.AwsAccessKey = loo_Session.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Session.StringOf("secret_key")
loo_Http.AwsSessionToken = loo_Session.StringOf("session_token")
loo_Http.AwsRegion = "us-west-2"
loo_Http.AwsEndpoint = "s3.us-west-2.amazonaws.com"
// Current Chilkat S3 methods use Signature Version 4.
loo_ObjectData = create oleobject
li_rc = loo_ObjectData.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Http.S3_DownloadBd(loo_Download.StringOf("bucket"),loo_Download.StringOf("object_key"),loo_ObjectData)
if li_Success <> 1 then
Write-Debug "S3 download failed. HTTP status: " + string(loo_Http.LastStatus)
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
destroy loo_Http
destroy loo_ObjectData
return
end if
if loo_Http.LastStatus <> 200 then
Write-Debug "Unexpected S3 status; do not use the data."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
destroy loo_Http
destroy loo_ObjectData
return
end if
Write-Debug "Downloaded bytes: " + string(loo_ObjectData.NumBytes)
// Consume objectData in memory; no object contents or credentials are logged.
Write-Debug "Example completed."
destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Download
destroy loo_Http
destroy loo_ObjectData