Sample code for 30+ languages & platforms
Rust

S3 Add Tags to an Object

See more Amazon S3 (new) Examples

Demonstrates how to add one or more tags to an S3 object.

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 in the desired region.
// (for us-east-1, we use "s3.amazonaws.com", but for another region, such as us-west-2, we would use "s3-us-west-2.amazonaws.com")
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.
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");
auth_aws.set_region("us-east-1");

let _ = rest.set_auth_aws(&auth_aws);

// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat100".
// Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-east-1.amazonaws.com"
// The same applies to aother regions.  The Host header should simply be <bucketName>.s3.amazonaws.com regardless of the region.
rest.set_host("chilkat100.s3.amazonaws.com");

let xml = chilkat::Xml::new();
xml.set_tag("Tagging");
xml.update_child_content("TagSet|Tag|Key", "plant");
xml.update_child_content("TagSet|Tag|Value", "chili pepper");

let sb_request_body = chilkat::StringBuilder::new();
let _ = xml.get_xml_sb(&sb_request_body);

// It is important to add the terminating "=" after the "?tagging".
let sb_response = chilkat::StringBuilder::new();
if rest.full_request_sb("PUT", "/chiliPepper.gif?tagging=", &sb_request_body, &sb_response).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

println!("Response status code: {}", rest.response_status_code());

// When successful, the S3 Storage service will respond with a 200 response code,
// with an XML body.  
if rest.response_status_code() != 200 {
    // Examine the request/response to see what happened.
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response body: {}", sb_response.get_as_string().unwrap_or_default());
    println!("---");
    println!("LastRequestStartLine: {}", rest.last_request_start_line());
    println!("LastRequestHeader: {}", rest.last_request_header());
}

println!("{}", sb_response.get_as_string().unwrap_or_default());
println!("Success.");