Sample code for 30+ languages & platforms
Rust

Create S3 Bucket in a Region

See more Amazon S3 Examples

Demonstrates how to create an S3 bucket in a specified region. This example will create a bucket in the eu-west-2 region.

Chilkat Rust Downloads

Rust

// This example 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.
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();

// We'll send a PUT request having an XML body such as this:
// <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> 
//   <LocationConstraint>BucketRegion</LocationConstraint> 
//   </CreateBucketConfiguration>

// Create the XML body specifying the region as "eu-west-2"
let bucket_region = "eu-west-2".to_string();
let sb_bucket_region = chilkat::StringBuilder::new();
let _ = sb_bucket_region.append(&bucket_region);

// We only need to specify the LocationConstraint if the bucket's region is NOT us-east-1
let xml = chilkat::Xml::new();
if !sb_bucket_region.contents_equal("us-east-1", true) {
    xml.set_tag("CreateBucketConfiguration");
    let _ = xml.add_attribute("xmlns", "http://s3.amazonaws.com/doc/2006-03-01/");
    xml.update_child_content("LocationConstraint", "eu-west-2");
}

// --------------------------------------------------------------
// IMPORTANT: To create a bucket in the default us-east-1 region,
// do not add the LocationConstraint.  Adding a LocationConstraint of "us-east-1"
// causes an error "The specified location-constraint is not valid."
// By default, the bucket is created in us-east-1 by sending a PUT with an empty body.
// --------------------------------------------------------------

// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkateuwest2".
rest.set_host("chilkateuwest2.s3.amazonaws.com");

// Make the call to create the bucket.
let mut response_str = String::new();
if !sb_bucket_region.contents_equal("us-east-1", true) {
    response_str = rest.full_request_string("PUT", "/", &xml.get_xml().unwrap_or_default()).unwrap_or_default();
} else {
    // If the bucket is to be created in the us-east-1 region (the default region)
    // just send a PUT with no body.
    response_str = rest.full_request_no_body("PUT", "/").unwrap_or_default();
}

if !rest.last_method_success() {
    println!("{}", rest.last_error_text());
    return;
}

if rest.response_status_code() != 200 {
    println!("status code = {}", rest.response_status_code());
    let response_xml = chilkat::Xml::new();
    let _ = response_xml.load_xml(&response_str);
    println!("{}", response_xml.get_xml().unwrap_or_default());
    println!("Failed.");
    return;
}

println!("Bucket in the eu-west-2 region created.");