Sample code for 30+ languages & platforms
Rust

S3 Upload Binary File from BinData

See more Amazon S3 (new) Examples

Upload a binary file contained in a BinData object to Amazon S3.

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;
// Make sure to connect to the region where the bucket is located..
let _ = rest.connect("s3-us-west-2.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");
// Use the correct region..
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("s3");

let _ = rest.set_auth_aws(&auth_aws).is_ok();

// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat.qa".
// (Also make sure to use the correct region.)
rest.set_host("chilkat.qa.s3-us-west-2.amazonaws.com");

// Load a text file into memory.
let png_data = chilkat::BinData::new();
if png_data.load_file("qa_data/png/anemone.png").is_err() {
    println!("Failed to load file from local filesystem.");
    return;
}

// Indicate the Content-Type of our upload.  (This is optional)
let _ = rest.add_header("Content-Type", "image/png");

// Upload the file to Amazon S3.
let sb_response = chilkat::StringBuilder::new();
if rest.full_request_bd("PUT", "/images/sea_creatures/anemone.png", &png_data, &sb_response).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Did we get a 200 response indicating success?
let status_code = rest.response_status_code();
if status_code != 200 {
    println!("Error response: {}", sb_response.get_as_string().unwrap_or_default());
    println!("Status code: {}, Status text: {}", status_code, rest.response_status_text());
    return;
}

println!("File successfully uploaded.");