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

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    stringCompare.i = CkStringBuilder::ckCreate()
    If stringCompare.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Load a privately stored STS XML response; never print its contents.
    tokenXml.i = CkXml::ckCreate()
    If tokenXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkStrict(tokenXml, 1)
    success = CkXml::ckLoadXmlFile(tokenXml,"qa_data/tokens/aws_session_token.xml")
    If success <> 1
        Debug "Could not load private STS XML."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        ProcedureReturn
    EndIf

    credentialPath.s = ""
    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch1.i = CkStringBuilder::ckContentsEqual(stringCompare,"GetSessionTokenResponse",1)
    If stringMatch1 = 1
        credentialPath = "GetSessionTokenResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch2.i = CkStringBuilder::ckContentsEqual(stringCompare,"AssumeRoleResponse",1)
    If stringMatch2 = 1
        credentialPath = "AssumeRoleResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch3.i = CkStringBuilder::ckContentsEqual(stringCompare,"AssumeRoleWithWebIdentityResponse",1)
    If stringMatch3 = 1
        credentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch4.i = CkStringBuilder::ckContentsEqual(stringCompare,"AssumeRoleWithSAMLResponse",1)
    If stringMatch4 = 1
        credentialPath = "AssumeRoleWithSAMLResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch5.i = CkStringBuilder::ckContentsEqual(stringCompare,"GetFederationTokenResponse",1)
    If stringMatch5 = 1
        credentialPath = "GetFederationTokenResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch6.i = CkStringBuilder::ckContentsEqual(stringCompare,"GetDelegatedAccessTokenResponse",1)
    If stringMatch6 = 1
        credentialPath = "GetDelegatedAccessTokenResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(tokenXml))
    stringMatch7.i = CkStringBuilder::ckContentsEqual(stringCompare,"AssumeRootResponse",1)
    If stringMatch7 = 1
        credentialPath = "AssumeRootResult|Credentials"
    EndIf

    CkStringBuilder::ckSetString(stringCompare,credentialPath)
    stringMatch8.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If stringMatch8 = 1
        Debug "Unsupported STS credential response."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        ProcedureReturn
    EndIf

    accessKey.s
    secretKey.s
    sessionToken.s
    expiration.s
    path.i = CkStringBuilder::ckCreate()
    If path.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckSetString(path,credentialPath)
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(path,"|AccessKeyId")
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    accessKey = CkXml::ckGetChildContent(tokenXml,CkStringBuilder::ckGetAsString(path))
    CkStringBuilder::ckSetString(stringCompare,accessKey)
    stringMatch9.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(tokenXml) <> 1) OR (stringMatch9 = 1)
        Debug "Missing credential field AccessKeyId."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckSetString(path,credentialPath)
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(path,"|SecretAccessKey")
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    secretKey = CkXml::ckGetChildContent(tokenXml,CkStringBuilder::ckGetAsString(path))
    CkStringBuilder::ckSetString(stringCompare,secretKey)
    stringMatch10.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(tokenXml) <> 1) OR (stringMatch10 = 1)
        Debug "Missing credential field SecretAccessKey."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckSetString(path,credentialPath)
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(path,"|SessionToken")
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    sessionToken = CkXml::ckGetChildContent(tokenXml,CkStringBuilder::ckGetAsString(path))
    CkStringBuilder::ckSetString(stringCompare,sessionToken)
    stringMatch11.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(tokenXml) <> 1) OR (stringMatch11 = 1)
        Debug "Missing credential field SessionToken."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckSetString(path,credentialPath)
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckAppend(path,"|Expiration")
    If success <> 1
        Debug "Could not construct credential path."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    expiration = CkXml::ckGetChildContent(tokenXml,CkStringBuilder::ckGetAsString(path))
    CkStringBuilder::ckSetString(stringCompare,expiration)
    stringMatch12.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(tokenXml) <> 1) OR (stringMatch12 = 1)
        Debug "Missing credential field Expiration."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        ProcedureReturn
    EndIf

    expires.i = CkDateTime::ckCreate()
    If expires.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    now.i = CkDateTime::ckCreate()
    If now.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkDateTime::ckSetFromTimestamp(expires,expiration)
    If success <> 1
        Debug "Stored expiration is invalid."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        ProcedureReturn
    EndIf

    success = CkDateTime::ckSetFromCurrentSystemTime(now)
    If success <> 1
        Debug "Could not read current time."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        ProcedureReturn
    EndIf

    If CkDateTime::ckDiffSeconds(expires,now) <= 60
        Debug "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        ProcedureReturn
    EndIf

    session.i = CkJsonObject::ckCreate()
    If session.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(session,"access_key",accessKey)
    If success <> 1
        Debug "Could not build normalized session."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(session,"secret_key",secretKey)
    If success <> 1
        Debug "Could not build normalized session."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(session,"session_token",sessionToken)
    If success <> 1
        Debug "Could not build normalized session."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckUpdateString(session,"expiration",expiration)
    If success <> 1
        Debug "Could not build normalized session."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        ProcedureReturn
    EndIf

    download.i = CkJsonObject::ckCreate()
    If download.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(download, 1)
    success = CkJsonObject::ckLoadFile(download,"qa_data/aws/sts_download.json")
    If success <> 1
        Debug "Could not load download configuration."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        CkJsonObject::ckDispose(download)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(download,"bucket"))
    stringMatch13.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(download,"bucket") <> 1) OR (stringMatch13 = 1)
        Debug "Missing bucket."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        CkJsonObject::ckDispose(download)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(download,"object_key"))
    stringMatch14.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(download,"object_key") <> 1) OR (stringMatch14 = 1)
        Debug "Missing object_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        CkJsonObject::ckDispose(download)
        ProcedureReturn
    EndIf

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkFollowRedirects(http, 0)
    CkHttp::setCkConnectTimeout(http, 20)
    CkHttp::setCkReadTimeout(http, 60)
    CkHttp::setCkVerboseLogging(http, 0)
    CkHttp::setCkAwsAccessKey(http, CkJsonObject::ckStringOf(session,"access_key"))
    CkHttp::setCkAwsSecretKey(http, CkJsonObject::ckStringOf(session,"secret_key"))
    CkHttp::setCkAwsSessionToken(http, CkJsonObject::ckStringOf(session,"session_token"))
    CkHttp::setCkAwsRegion(http, "us-west-2")
    CkHttp::setCkAwsEndpoint(http, "s3.us-west-2.amazonaws.com")
    ;  Current Chilkat S3 methods use Signature Version 4.
    objectData.i = CkBinData::ckCreate()
    If objectData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckS3_DownloadBd(http,CkJsonObject::ckStringOf(download,"bucket"),CkJsonObject::ckStringOf(download,"object_key"),objectData)
    If success <> 1
        Debug "S3 download failed. HTTP status: " + Str(CkHttp::ckLastStatus(http))
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        CkJsonObject::ckDispose(download)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(objectData)
        ProcedureReturn
    EndIf

    If CkHttp::ckLastStatus(http) <> 200
        Debug "Unexpected S3 status; do not use the data."
        CkStringBuilder::ckDispose(stringCompare)
        CkXml::ckDispose(tokenXml)
        CkStringBuilder::ckDispose(path)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkJsonObject::ckDispose(session)
        CkJsonObject::ckDispose(download)
        CkHttp::ckDispose(http)
        CkBinData::ckDispose(objectData)
        ProcedureReturn
    EndIf

    Debug "Downloaded bytes: " + Str(CkBinData::ckNumBytes(objectData))
    ;  Consume objectData in memory; no object contents or credentials are logged.
    Debug "Example completed."


    CkStringBuilder::ckDispose(stringCompare)
    CkXml::ckDispose(tokenXml)
    CkStringBuilder::ckDispose(path)
    CkDateTime::ckDispose(expires)
    CkDateTime::ckDispose(now)
    CkJsonObject::ckDispose(session)
    CkJsonObject::ckDispose(download)
    CkHttp::ckDispose(http)
    CkBinData::ckDispose(objectData)


    ProcedureReturn
EndProcedure