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 Lua Downloads
local chilkat = require("chilkat")
local stringCompare = chilkat.StringBuilder.new()
-- Load a privately stored STS XML response; never print its contents.
local tokenXml = chilkat.Xml.new()
tokenXml.strict = true
local success = tokenXml:load_xml_file("qa_data/tokens/aws_session_token.xml")
if not success then
print("Could not load private STS XML.")
return
end
local credentialPath = ""
stringCompare:set_string(tokenXml.tag)
local stringMatch1 = stringCompare:contents_equal("GetSessionTokenResponse", true)
if stringMatch1 then
credentialPath = "GetSessionTokenResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch2 = stringCompare:contents_equal("AssumeRoleResponse", true)
if stringMatch2 then
credentialPath = "AssumeRoleResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch3 = stringCompare:contents_equal("AssumeRoleWithWebIdentityResponse", true)
if stringMatch3 then
credentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch4 = stringCompare:contents_equal("AssumeRoleWithSAMLResponse", true)
if stringMatch4 then
credentialPath = "AssumeRoleWithSAMLResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch5 = stringCompare:contents_equal("GetFederationTokenResponse", true)
if stringMatch5 then
credentialPath = "GetFederationTokenResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch6 = stringCompare:contents_equal("GetDelegatedAccessTokenResponse", true)
if stringMatch6 then
credentialPath = "GetDelegatedAccessTokenResult|Credentials"
end
stringCompare:set_string(tokenXml.tag)
local stringMatch7 = stringCompare:contents_equal("AssumeRootResponse", true)
if stringMatch7 then
credentialPath = "AssumeRootResult|Credentials"
end
stringCompare:set_string(credentialPath)
local stringMatch8 = stringCompare:contents_equal("", true)
if stringMatch8 then
print("Unsupported STS credential response.")
return
end
local path = chilkat.StringBuilder.new()
success = path:set_string(credentialPath)
if not success then
print("Could not construct credential path.")
return
end
success = path:append("|AccessKeyId")
if not success then
print("Could not construct credential path.")
return
end
local accessKey = tokenXml:get_child_content(path:get_as_string())
stringCompare:set_string(accessKey)
local stringMatch9 = stringCompare:contents_equal("", true)
if (not tokenXml.last_method_success) or (stringMatch9) then
print("Missing credential field AccessKeyId.")
return
end
success = path:set_string(credentialPath)
if not success then
print("Could not construct credential path.")
return
end
success = path:append("|SecretAccessKey")
if not success then
print("Could not construct credential path.")
return
end
local secretKey = tokenXml:get_child_content(path:get_as_string())
stringCompare:set_string(secretKey)
local stringMatch10 = stringCompare:contents_equal("", true)
if (not tokenXml.last_method_success) or (stringMatch10) then
print("Missing credential field SecretAccessKey.")
return
end
success = path:set_string(credentialPath)
if not success then
print("Could not construct credential path.")
return
end
success = path:append("|SessionToken")
if not success then
print("Could not construct credential path.")
return
end
local sessionToken = tokenXml:get_child_content(path:get_as_string())
stringCompare:set_string(sessionToken)
local stringMatch11 = stringCompare:contents_equal("", true)
if (not tokenXml.last_method_success) or (stringMatch11) then
print("Missing credential field SessionToken.")
return
end
success = path:set_string(credentialPath)
if not success then
print("Could not construct credential path.")
return
end
success = path:append("|Expiration")
if not success then
print("Could not construct credential path.")
return
end
local expiration = tokenXml:get_child_content(path:get_as_string())
stringCompare:set_string(expiration)
local stringMatch12 = stringCompare:contents_equal("", true)
if (not tokenXml.last_method_success) or (stringMatch12) then
print("Missing credential field Expiration.")
return
end
local expires = chilkat.DateTime.new()
local now = chilkat.DateTime.new()
success = expires:set_from_timestamp(expiration)
if not success then
print("Stored expiration is invalid.")
return
end
success = now:set_from_current_system_time()
if not success then
print("Could not read current time.")
return
end
if expires:diff_seconds(now) <= 60 then
print("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.")
return
end
local session = chilkat.JsonObject.new()
success = session:update_string("access_key", accessKey)
if not success then
print("Could not build normalized session.")
return
end
success = session:update_string("secret_key", secretKey)
if not success then
print("Could not build normalized session.")
return
end
success = session:update_string("session_token", sessionToken)
if not success then
print("Could not build normalized session.")
return
end
success = session:update_string("expiration", expiration)
if not success then
print("Could not build normalized session.")
return
end
local download = chilkat.JsonObject.new()
download.strict = true
success = download:load_file("qa_data/aws/sts_download.json")
if not success then
print("Could not load download configuration.")
return
end
stringCompare:set_string(download:string_of("bucket"))
local stringMatch13 = stringCompare:contents_equal("", true)
if (download:json_type_of("bucket") ~= 1) or (stringMatch13) then
print("Missing bucket.")
return
end
stringCompare:set_string(download:string_of("object_key"))
local stringMatch14 = stringCompare:contents_equal("", true)
if (download:json_type_of("object_key") ~= 1) or (stringMatch14) then
print("Missing object_key.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 20
http.read_timeout = 60
http.verbose_logging = false
http.aws_access_key = session:string_of("access_key")
http.aws_secret_key = session:string_of("secret_key")
http.aws_session_token = session:string_of("session_token")
http.aws_region = "us-west-2"
http.aws_endpoint = "s3.us-west-2.amazonaws.com"
-- Current Chilkat S3 methods use Signature Version 4.
local objectData = chilkat.BinData.new()
success = http:s3_download_bd(download:string_of("bucket"), download:string_of("object_key"), objectData)
if not success then
print("S3 download failed. HTTP status: " .. http.last_status)
return
end
if http.last_status ~= 200 then
print("Unexpected S3 status; do not use the data.")
return
end
print("Downloaded bytes: " .. objectData.num_bytes)
-- Consume objectData in memory; no object contents or credentials are logged.
print("Example completed.")