Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the Azure Storage Blob Service
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
// In this example, the storage account name is "chilkat".
rest.connect("chilkat.blob.core.windows.net", port, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Provide Azure Cloud credentials for the REST call.
const az_auth = try chilkat.AuthAzureStorage.init();
defer az_auth.deinit();
az_auth.setAccessKey("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.setAccount("chilkat");
az_auth.setScheme("SharedKey");
az_auth.setService("Blob");
// This causes the "x-ms-version: 2021-08-06" header to be automatically added.
az_auth.setXMsVersion("2021-08-06");
try rest.setAuthAzureStorage(az_auth);
// 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..
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.setTag("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
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
try fac.openForRead("qa_data/xml/hamlet.xml");
// 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...)
const block_size = 16384;
// How many 16K blocks? (Including 1 for the last partial block)
const num_blocks = fac.getNumBlocks(block_size);
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setHashAlgorithm("md5");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
const uri_path = try chilkat.StringBuilder.init();
defer uri_path.deinit();
var block_id: [:0]const u8 = "";
const data_block = try chilkat.BinData.init();
defer data_block.deinit();
var content_md5: [:0]const u8 = "";
var i: i32 = 0;
while (i < num_blocks) {
data_block.clear() catch {};
fac.readBlockBd(i, block_size, data_block) catch {
std.debug.print("{s}\n", .{try fac.getLastErrorText(alloc)});
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 = try fac.genBlockId(alloc, i, 4, "base64");
// Add this blockId to the list of blocks to be committed.
xml.newChild2("Latest", block_id);
// Build the URI path
uri_path.clear();
try uri_path.append("/mycontainer/hamlet.xml?comp=block&blockId=");
try uri_path.append(block_id);
content_md5 = try crypt.hashBdENC(alloc, data_block);
rest.addHeader("Content-MD5", content_md5) catch {};
// Upload this block..
sb_response_body.clear();
rest.fullRequestBd("PUT", try uri_path.getAsString(alloc), data_block, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Verify that we received a 201 status code.
if (rest.getResponseStatusCode() != 201) {
// Examine the request/response to see what happened.
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body (if any): {s}\n", .{try sb_response_body.getAsString(alloc)});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
i = i + 1;
}
fac.fileClose();
// Now commit the blocks.
// Let's have a look at the XML that will commit the blocks:
const xml_str = try xml.getXml(alloc);
std.debug.print("{s}\n", .{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.
// --------------------------------------------------------------------------
rest.removeHeader("Content-MD5") catch {};
// Send the PUT Block List...
const response_str = rest.fullRequestString(alloc, "PUT", "/mycontainer/hamlet.xml?comp=blocklist", xml_str) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// When successful, the Azure Storage service will respond with a 201 response status code,
// with no response body.
if (rest.getResponseStatusCode() != 201) {
// Examine the request/response to see what happened.
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body (if any): {s}\n", .{response_str});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
std.debug.print("Success.\n", .{});
}