Rust
Rust
S3 List Bucket Objects (Bucket-in-Path)
See more Amazon S3 (new) Examples
Demonstrates how to fetch a list of objects in an S3 bucket, but using the bucket-in-path request format instead of putting the bucket name in the HOST header.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.
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();
// ----------------------------------------------------------------------------
// Important: For buckets created in regions outside us-east-1,
// there are three important changes that need to be made.
// See Working with S3 Buckets in Non-us-east-1 Regions for the details.
// ----------------------------------------------------------------------------
// 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 is "chilkat100"
let sb_response = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("GET", "/chilkat100", &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
let status_code = rest.response_status_code();
println!("Response status code = {}", status_code);
let xml = chilkat::Xml::new();
let _ = xml.load_sb(&sb_response, true);
println!("{}", xml.get_xml().unwrap_or_default());
if status_code != 200 {
println!("Failed. See error information in the XML.");
return;
}
// Use this online tool to generate code from sample XML:
// Generate Code to Create XML
let name = xml.get_child_content("Name").unwrap_or_default();
let max_keys = xml.get_child_int_value("MaxKeys");
let is_truncated = xml.get_child_content("IsTruncated").unwrap_or_default();
let mut i = 0;
let count_i = xml.num_children_having_tag("Contents");
while i < count_i {
xml.set_i(i);
let _ = xml.get_child_content("Contents[i]|Key").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|LastModified").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|ETag").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|Size").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|Owner|ID").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|Owner|DisplayName").unwrap_or_default();
let _ = xml.get_child_content("Contents[i]|StorageClass").unwrap_or_default();
i = i + 1;
}
println!("Success.");