Rust
Rust
S3 Rename File
See more Amazon S3 (new) Examples
Demonstrates how to rename a file on Amazon S3.Renaming an object (file) in AWS S3 using the AWS S3 API involves copying the object to a new location with the desired name and then deleting the original object.
Chilkat Rust Downloads
// 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.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("s3-us-west-2.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");
// This particular bucket is in the Oregon region, as opposed to the US Standard,
// therefore the Region must be set appropriately.
// Also note that we connected to "s3-us-west-2.amazonaws.com".
auth_aws.set_region("us-west-2");
let _ = rest.set_auth_aws(&auth_aws);
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat.qa".
// Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-west-2.amazonaws.com"
rest.set_host("chilkat.qa.s3.amazonaws.com");
// Copy /images/sea_creatures/starfish.jpg to /images/sea_creatures/starfish3.jpg
// Notice here that the x-amz-copy-source includes the bucket in the source path,
// but the target path passed to FullRequestNoBody does NOT contain the bucket (because the bucket
// was already specified in the Host header).
let _ = rest.add_header("x-amz-copy-source", "/chilkat.qa/images/sea_creatures/starfish.jpg");
let sb_response = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("PUT", "/images/sea_creatures/starfish3.jpg", &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
// When successful, the S3 Storage service will respond with a 200 or 204 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!("Copy Successful.");
// --------------------------------------
// Now we can delete the original file..
// --------------------------------------
// We no longer want to send the x-amz-copy-source header. Remove it.
let _ = rest.remove_header("x-amz-copy-source");
if rest.full_request_no_body_sb("DELETE", "/images/sea_creatures/starfish.jpg", &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
// If successfully deleted, the response status code is 204 and the response body text will be empty.
let status_code = rest.response_status_code();
println!("Response status code = {}", status_code);
println!("Response text = ");
println!("{}", sb_response.get_as_string().unwrap_or_default());