Sample code for 30+ languages & platforms
Rust

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

Rust

// 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 xml_token = chilkat::Xml::new();
if xml_token.load_xml_file("qa_data/tokens/aws_session_token.xml").is_err() {
    println!("{}", xml_token.last_error_text());
    return;
}

let rest = chilkat::Rest::new();

// Connect to the Amazon AWS REST server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("s3.amazonaws.com", port, b_tls, b_auto_reconnect).is_ok();

// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();

// The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
auth_aws.set_access_key(&xml_token.get_child_content("GetSessionTokenResult|Credentials|AccessKeyId").unwrap_or_default());
auth_aws.set_secret_key(&xml_token.get_child_content("GetSessionTokenResult|Credentials|SecretAccessKey").unwrap_or_default());
auth_aws.set_service_name("s3");
let _ = rest.set_auth_aws(&auth_aws).is_ok();

let _ = rest.add_query_param("X-Amz-Security-Token", &xml_token.get_child_content("GetSessionTokenResult|Credentials|SessionToken").unwrap_or_default());

let sb_response = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("GET", "/", &sb_response).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let status_code = rest.response_status_code();
println!("Response status code = {}", status_code);

let xml = chilkat::Xml::new();
let _ = xml.load_sb(&sb_response, true);
println!("{}", xml.get_xml().unwrap_or_default());

if status_code != 200 {
    println!("Failed.  See error information in the XML.");
    return;
}