Sample code for 30+ languages & platforms
Rust

Upload File in Blocks (with Content-MD5 header) and Commit the Block List

See more Azure Cloud Storage Examples

Demonstrates how to upload a file in blocks and then commit the block list. This example includes a Content-MD5 header for each block.

Chilkat Rust Downloads

Rust

// Azure Blob Service Example: Upload a file in blocks, and then commit the block list.
// See also: https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx

// 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 Azure Storage Blob Service
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
// In this example, the storage account name is "chilkat".
if rest.connect("chilkat.blob.core.windows.net", port, b_tls, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Provide Azure Cloud credentials for the REST call.
let az_auth = chilkat::AuthAzureStorage::new();
az_auth.set_access_key("AZURE_ACCESS_KEY");
// The account name used here should match the 1st part of the domain passed in the call to Connect (above).
az_auth.set_account("chilkat");
az_auth.set_scheme("SharedKey");
az_auth.set_service("Blob");
// This causes the "x-ms-version: 2021-08-06" header to be automatically added.
az_auth.set_x_ms_version("2021-08-06");
let _ = rest.set_auth_azure_storage(&az_auth).is_ok();

// Note: The application does not need to explicitly set the following
// headers: Content-Length, x-ms-date, Authorization.  These headers
// are automatically set by Chilkat.

// As the blocks are uploaded, we'll keep an XML block list for the subsequent commit..
let xml = chilkat::Xml::new();
xml.set_tag("BlockList");

// Any type of file can be uploaded in this way.  It can a text file, binary file, anything...
// This example will upload an XML file that is approximately 275K in size.  It can be downloaded
// at http://www.chilkatsoft.com/hamlet.xml
let fac = chilkat::FileAccess::new();
let _ = fac.open_for_read("qa_data/xml/hamlet.xml").is_ok();
// Assuming success for the example..

// We'll upload in 16K blocks (normally a program would upload in larger block sizes than this,
// but this is just an example...)
let block_size = 16384;

// How many 16K blocks?  (Including 1 for the last partial block)
let num_blocks = fac.get_num_blocks(block_size);

let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("md5");

let sb_response_body = chilkat::StringBuilder::new();
let uri_path = chilkat::StringBuilder::new();
let mut block_id = String::new();
let data_block = chilkat::BinData::new();
let mut content_md5 = String::new();
let mut i = 0;
while i < num_blocks {

    let _ = data_block.clear();
    if fac.read_block_bd(i, block_size, &data_block).is_err() {
        println!("{}", fac.last_error_text());
        return;
    }

    // Generate a base64 block ID.  
    // (Chilkat provides a helper method named GenBlockId to make this easy)
    // A pre-base64 encoded block ID length of 4 is sufficient in this case because
    // this file certainly won't have more than 99,999 blocks..
    block_id = fac.gen_block_id(i, 4, "base64").unwrap_or_default();

    // Add this blockId to the list of blocks to be committed.
    xml.new_child2("Latest", &block_id);

    // Build the URI path
    uri_path.clear();
    let _ = uri_path.append("/mycontainer/hamlet.xml?comp=block&blockId=").is_ok();
    let _ = uri_path.append(&block_id).is_ok();

    content_md5 = crypt.hash_bd_enc(&data_block).unwrap_or_default();
    let _ = rest.add_header("Content-MD5", &content_md5);

    // Upload this block..
    sb_response_body.clear();
    if rest.full_request_bd("PUT", &uri_path.get_as_string().unwrap_or_default(), &data_block, &sb_response_body).is_err() {
        println!("{}", rest.last_error_text());
        return;
    }

    // Verify that we received a 201 status code.
    if rest.response_status_code() != 201 {
        // 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 (if any): {}", sb_response_body.get_as_string().unwrap_or_default());
        println!("---");
        println!("LastRequestStartLine: {}", rest.last_request_start_line());
        println!("LastRequestHeader: {}", rest.last_request_header());
        return;
    }

    i = i + 1;
}

fac.file_close();

// Now commit the blocks.
// Let's have a look at the XML that will commit the blocks:
let xml_str = xml.get_xml().unwrap_or_default();
println!("{}", xml_str);

// The XML will look like this:

// <?xml version="1.0" encoding="utf-8" ?>
// <BlockList>
//     <Latest>MDAwMA==</Latest>
//     <Latest>MDAwMQ==</Latest>
//     <Latest>MDAwMg==</Latest>
//     <Latest>MDAwMw==</Latest>
//     <Latest>MDAwNA==</Latest>
//     <Latest>MDAwNQ==</Latest>
//     <Latest>MDAwNg==</Latest>
//     <Latest>MDAwNw==</Latest>
//     <Latest>MDAwOA==</Latest>
//     <Latest>MDAwOQ==</Latest>
//     <Latest>MDAxMA==</Latest>
//     <Latest>MDAxMQ==</Latest>
//     <Latest>MDAxMg==</Latest>
//     <Latest>MDAxMw==</Latest>
//     <Latest>MDAxNA==</Latest>
//     <Latest>MDAxNQ==</Latest>
//     <Latest>MDAxNg==</Latest>
//     <Latest>MDAxNw==</Latest>
// </BlockList>

// --------------------------------------------------------------------------
// IMPORTANT: Remove the Content-MD5 header previously set in the loop above.
// --------------------------------------------------------------------------
let _ = rest.remove_header("Content-MD5");

// Send the PUT Block List...
let Ok(response_str) = rest.full_request_string("PUT", "/mycontainer/hamlet.xml?comp=blocklist", &xml_str) else {
    println!("{}", rest.last_error_text());
    return;
};

// When successful, the Azure Storage service will respond with a 201 response status code,
// with no response body.

if rest.response_status_code() != 201 {
    // 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 (if any): {}", response_str);
    println!("---");
    println!("LastRequestStartLine: {}", rest.last_request_start_line());
    println!("LastRequestHeader: {}", rest.last_request_header());
    return;
}

println!("Success.");