Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

-- 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.
local xmlToken = chilkat.Xml.new()
local success = xmlToken:load_xml_file("qa_data/tokens/aws_session_token.xml")
if not success then
    print(xmlToken.last_error_text)
    return
end

local rest = chilkat.Rest.new()

-- Connect to the Amazon AWS REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
success = rest:connect("s3.amazonaws.com", port, bTls, bAutoReconnect)

-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()

-- The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
authAws.access_key = xmlToken:get_child_content("GetSessionTokenResult|Credentials|AccessKeyId")
authAws.secret_key = xmlToken:get_child_content("GetSessionTokenResult|Credentials|SecretAccessKey")
authAws.service_name = "s3"
success = rest:set_auth_aws(authAws)

rest:add_query_param("X-Amz-Security-Token", xmlToken:get_child_content("GetSessionTokenResult|Credentials|SessionToken"))

local sbResponse = chilkat.StringBuilder.new()
success = rest:full_request_no_body_sb("GET", "/", sbResponse)
if not success then
    print(rest.last_error_text)
    return
end

local statusCode = rest.response_status_code
print("Response status code = " .. statusCode)

local xml = chilkat.Xml.new()
xml:load_sb(sbResponse, true)
print(xml:get_xml())

if statusCode ~= 200 then
    print("Failed.  See error information in the XML.")
    return
end