Rust
Rust
S3 Find Bucket Region
See more Amazon S3 (new) Examples
Demonstrates how to find out the region of an S3 bucket.Chilkat Rust Downloads
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = chilkat::Rest::new();
// Connect to the Amazon AWS REST server using the correct region (in this example, "us-west-2")
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();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
auth_aws.set_service_name("s3");
let _ = rest.set_auth_aws(&auth_aws).is_ok();
// The bucket name we want to know about is set in the HOST header.
// In this example, we want to find the region for the "chilkat.qa" bucket.
rest.set_host("chilkat.qa.s3.amazonaws.com");
// Send the GET request to query the bucket location.
let Ok(str_result) = rest.full_request_no_body("GET", "/?location") else {
println!("{}", rest.last_error_text());
return;
};
let response_status_code = rest.response_status_code();
println!("Response status code = {}", response_status_code);
if response_status_code != 200 {
println!("{}", rest.response_header());
println!("{}", str_result);
println!("Failed.");
return;
}
println!("{}", str_result);
// The result contains XML like this:
// <?xml version="1.0" encoding="UTF-8"?>
// <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">us-west-2</LocationConstraint>
// Get the "us-west-2" from the XML
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&str_result).is_ok();
println!("region = {}", xml.content());