Sample code for 30+ languages & platforms
Rust

S3 Resume Download

See more Amazon S3 (new) Examples

Suppose an S3 download of a very large file failed for some reason and you have a partial file on disk. Rather than restart the entire download, you wish to download the remaining portion. This example demonstrates how to finish a previously failed download.

Note: This example requires Chilkat v9.5.0.83 or above.

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();

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

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

// We want to continue downloading a file.
// The relative local filepath of our previously partially downoaded file is: qa_output/hamlet.xml
// Let's find out how many bytes are already downloaded.
let fac = chilkat::FileAccess::new();
let local_filepath = "qa_output/hamlet.xml".to_string();
// Note: The FileSize method returns a signed 32-bit integer.  If the file is potentially larger than 2GB, call FileSizeStr instead to return
// the size of the file as a string, then convert to an integer value.
let sz = fac.file_size(&local_filepath);
if sz > 0 {
    println!("sz = {}", sz);

    // If the sz equals 42375 bytes, then we want to add a Range header that looks like this:
    // Range: bytes=42375-
    let sb_range = chilkat::StringBuilder::new();
    let _ = sb_range.append("bytes=");
    let _ = sb_range.append_int(sz);
    let _ = sb_range.append("-");
    let _ = rest.add_header("Range", &sb_range.get_as_string().unwrap_or_default());
    println!("Added Range: {}", sb_range.get_as_string().unwrap_or_default());
}

// Send the request to download the remainder of the file.
if rest.send_req_no_body("GET", "/hamlet.xml").is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Read the response header.
let response_status_code = rest.read_response_header();
if response_status_code < 0 {
    println!("{}", rest.last_error_text());
    return;
}

println!("Response status code = {}", response_status_code);

// We expect a 200 or 206 response status if the file data is coming.
// Otherwise, we'll get a string response body with an error message(or no response body).
if (response_status_code == 200) || (response_status_code == 206) {

    let body_stream = chilkat::Stream::new();

    // The stream's sink will be a file.
    // We will append to the file..
    body_stream.set_sink_file(&local_filepath);

    // Indicate that we wish to append to the output file.
    // The SinkFileAppend property was added in Chilkat v9.50.83
    body_stream.set_sink_file_append(true);

    // Read the response body to the stream.  Given that we've
    // set the stream's sink to a file, it will stream directly
    // to the file.
    if rest.read_resp_body_stream(&body_stream, true).is_err() {
        println!("{}", rest.last_error_text());
        return;
    }

    println!("Successfully downloaded the file.");

} else {
    let err_response = rest.read_resp_body_string().unwrap_or_default();
    if !rest.last_method_success() {
        println!("{}", rest.last_error_text());
    } else {
        println!("{}", err_response);
    }

}