Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

// 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.
loXmlToken = createobject("CkXml")
llSuccess = loXmlToken.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
if (llSuccess = .F.) then
    ? loXmlToken.LastErrorText
    release loXmlToken
    return
endif

loRest = createobject("CkRest")

// Connect to the Amazon AWS REST server.
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.
llSuccess = loRest.Connect("s3.amazonaws.com",lnPort,llBTls,llBAutoReconnect)

// Provide AWS credentials for the REST call.
loAuthAws = createobject("CkAuthAws")

// The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
loAuthAws.AccessKey = loXmlToken.GetChildContent("GetSessionTokenResult|Credentials|AccessKeyId")
loAuthAws.SecretKey = loXmlToken.GetChildContent("GetSessionTokenResult|Credentials|SecretAccessKey")
loAuthAws.ServiceName = "s3"
llSuccess = loRest.SetAuthAws(loAuthAws)

loRest.AddQueryParam("X-Amz-Security-Token",loXmlToken.GetChildContent("GetSessionTokenResult|Credentials|SessionToken"))

loSbResponse = createobject("CkStringBuilder")
llSuccess = loRest.FullRequestNoBodySb("GET","/",loSbResponse)
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loXmlToken
    release loRest
    release loAuthAws
    release loSbResponse
    return
endif

lnStatusCode = loRest.ResponseStatusCode
? "Response status code = " + str(lnStatusCode)

loXml = createobject("CkXml")
loXml.LoadSb(loSbResponse,.T.)
? loXml.GetXml()

if (lnStatusCode <> 200) then
    ? "Failed.  See error information in the XML."
    release loXmlToken
    release loRest
    release loAuthAws
    release loSbResponse
    release loXml
    return
endif



release loXmlToken
release loRest
release loAuthAws
release loSbResponse
release loXml