Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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.
let xmlToken = CkoXml()!
success = xmlToken.loadFile(path: "qa_data/tokens/aws_session_token.xml")
if success == false {
print("\(xmlToken.lastErrorText!)")
return
}
let rest = CkoRest()!
// Connect to the Amazon AWS REST server.
var bTls: Bool = true
var port: Int = 443
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "s3.amazonaws.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
// Provide AWS credentials for the REST call.
let authAws = CkoAuthAws()!
// The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
authAws.accessKey = xmlToken.getChildContent(tagPath: "GetSessionTokenResult|Credentials|AccessKeyId")
authAws.secretKey = xmlToken.getChildContent(tagPath: "GetSessionTokenResult|Credentials|SecretAccessKey")
authAws.serviceName = "s3"
success = rest.setAuthAws(authProvider: authAws)
rest.addQueryParam(name: "X-Amz-Security-Token", value: xmlToken.getChildContent(tagPath: "GetSessionTokenResult|Credentials|SessionToken"))
let sbResponse = CkoStringBuilder()!
success = rest.fullRequestNoBodySb(httpVerb: "GET", uriPath: "/", sb: sbResponse)
if success != true {
print("\(rest.lastErrorText!)")
return
}
var statusCode: Int = rest.responseStatusCode.intValue
print("Response status code = \(statusCode)")
let xml = CkoXml()!
xml.loadSb(sb: sbResponse, autoTrim: true)
print("\(xml.getXml()!)")
if statusCode != 200 {
print("Failed. See error information in the XML.")
return
}
}