Sample code for 30+ languages & platforms
Xbase++

S3 List Buckets using an STS Session Token

See more AWS Security Token Service Examples

This is an example showing how to use an STS session token in an Amazon AWS request. This example will list S3 buckets using a previously obtained session token.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXmlToken
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL oSbResponse
LOCAL nStatusCode
LOCAL oXml

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  See Get AWS STS Session Token for sample code to get the session token XML.
oXmlToken := CreateObject("Chilkat.Xml")
nSuccess := oXmlToken:LoadXmlFile("qa_data/tokens/aws_session_token.xml")
IF (nSuccess == 0)
    ? oXmlToken:LastErrorText
    oXmlToken:destroy()
    RETURN
ENDIF

oRest := CreateObject("Chilkat.Rest")

//  Connect to the Amazon AWS REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("s3.amazonaws.com", nPort, nBTls, nBAutoReconnect)

//  Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")

//  The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
oAuthAws:AccessKey := oXmlToken:GetChildContent("GetSessionTokenResult|Credentials|AccessKeyId")
oAuthAws:SecretKey := oXmlToken:GetChildContent("GetSessionTokenResult|Credentials|SecretAccessKey")
oAuthAws:ServiceName := "s3"
nSuccess := oRest:SetAuthAws(oAuthAws)

oRest:AddQueryParam("X-Amz-Security-Token", oXmlToken:GetChildContent("GetSessionTokenResult|Credentials|SessionToken"))

oSbResponse := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestNoBodySb("GET", "/", oSbResponse)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oXmlToken:destroy()
    oRest:destroy()
    oAuthAws:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

nStatusCode := oRest:ResponseStatusCode
? "Response status code = " + Str(nStatusCode)

oXml := CreateObject("Chilkat.Xml")
oXml:LoadSb(oSbResponse, 1)
? oXml:GetXml()

IF (nStatusCode != 200)
    ? "Failed.  See error information in the XML."
    oXmlToken:destroy()
    oRest:destroy()
    oAuthAws:destroy()
    oSbResponse:destroy()
    oXml:destroy()
    RETURN
ENDIF

oXmlToken:destroy()
oRest:destroy()
oAuthAws:destroy()
oSbResponse:destroy()
oXml:destroy()