Azure Blob: Resume a Block Upload from a Checkpoint
See more Azure Cloud Storage Examples
Persist the destination, source size/SHA-256, block size and unique upload ID before staging any blocks. On a later run, verify the source, get the uncommitted block list, skip matching blocks, upload missing blocks, and commit in source order. Block IDs are derived from the saved upload ID and fixed-width index, so a restart uses the same IDs.
Use one process per checkpoint and keep its source and destination exclusively controlled. The example does not replace an existing blob. Commit metadata lets a restart recognize its own completed upload after an uncertain response; otherwise an existing destination is an error. Remote block size is checked, but the source fingerprint and unique namespace are what associate the blocks with this upload. Stale uncommitted blocks can expire; missing blocks are uploaded again. Empty files commit an empty block list.
The sample uses 4 MiB blocks and at most 50,000 blocks; source sizes stay strings and block indexes use int. It fingerprints the whole source at start and before commit, trading read time for change detection. Keep checkpoint files private and unchanged. A corrupt checkpoint fails closed; choose a fresh checkpoint and destination deliberately rather than discarding state automatically.
Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.
Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.
Chilkat Node.js Downloads
NODEJS_PRELUDE
function chilkatExample() {
var success = false;
// StringBuilder compares text by value in every generated language.
var textCompare = new chilkat.StringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
var config = new chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/storage.json");
if (success !== true) {
console.log("Could not load private Azure configuration.");
return;
}
textCompare.Clear();
textCompare.SetString(config.StringOf("account"));
var sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") !== 1) || (sameText1 == true)) {
console.log("Supply the storage account name.");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.StringOf("access_key"));
var sameText2 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("access_key") !== 1) || (sameText2 == true)) {
console.log("Supply the base64 account access key.");
return;
}
var endpoint = new chilkat.StringBuilder();
success = endpoint.Append(config.StringOf("account"));
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = endpoint.Append(".blob.core.windows.net");
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
var resourcePath = "/examplecontainer/example.bin";
var rest = new chilkat.Rest();
rest.ConnectTimeoutMs = 20000;
rest.IdleTimeoutMs = 60000;
var azAuth = new chilkat.AuthAzureStorage();
azAuth.Account = config.StringOf("account");
azAuth.AccessKey = config.StringOf("access_key");
azAuth.Service = "Blob";
azAuth.Scheme = "SharedKey";
azAuth.XMsVersion = "2026-04-06";
success = rest.SetAuthAzureStorage(azAuth);
if (success !== true) {
console.log("Could not configure Shared Key signing.");
return;
}
var connection = new chilkat.Socket();
connection.RequireSslCertVerify = true;
connection.MaxReadIdleMs = 60000;
connection.MaxSendIdleMs = 60000;
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success !== true) {
console.log("TLS connection failed; inspect diagnostics privately.");
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
var responseText;
var responseBody = new chilkat.StringBuilder();
var result = new chilkat.Xml();
result.Strict = true;
var sourceFile = "qa_data/bin/sample.bin";
var checkpointFile = "qa_data/azure/upload-checkpoint.json";
var input = new chilkat.FileAccess();
success = input.OpenForRead(sourceFile);
if (success !== true) {
console.log("Could not open upload source.");
input.FileClose();
return;
}
var sourceSize = input.FileSizeStr(sourceFile);
if (input.LastMethodSuccess !== true) {
console.log("Could not read source size.");
input.FileClose();
return;
}
var blockSize = 4194304;
var numBlocks = input.GetNumBlocks(blockSize);
if ((input.LastMethodSuccess !== true) || (numBlocks < 0) || (numBlocks > 50000)) {
console.log("Source exceeds the 50,000-block demonstration bound.");
input.FileClose();
return;
}
var hash = new chilkat.Crypt2();
hash.HashAlgorithm = "sha256";
hash.EncodingMode = "hex_lower";
var fingerprint = hash.HashFileENC(sourceFile);
if (hash.LastMethodSuccess !== true) {
console.log("Could not fingerprint source.");
input.FileClose();
return;
}
var checkpoint = new chilkat.JsonObject();
checkpoint.Strict = true;
var files = new chilkat.FileAccess();
if (files.FileExists(checkpointFile) == true) {
success = checkpoint.LoadFile(checkpointFile);
if (success !== true) {
console.log("Invalid checkpoint; do not silently start another upload.");
input.FileClose();
return;
}
textCompare.Clear();
textCompare.SetString(checkpoint.StringOf("path"));
var sameText3 = textCompare.ContentsEqual(resourcePath,true);
textCompare.Clear();
textCompare.SetString(checkpoint.StringOf("sha256"));
var sameText4 = textCompare.ContentsEqual(fingerprint,true);
textCompare.Clear();
textCompare.SetString(checkpoint.StringOf("size"));
var sameText5 = textCompare.ContentsEqual(sourceSize,true);
if ((checkpoint.StringOf("account") !== config.StringOf("account")) || (sameText3 !== true) || (sameText4 !== true) || (sameText5 !== true) || (checkpoint.IntOf("block_size") !== blockSize)) {
console.log("Checkpoint target or source changed. Choose a new checkpoint/target deliberately.");
input.FileClose();
return;
}
textCompare.Clear();
textCompare.SetString(checkpoint.StringOf("upload_id"));
var sameText6 = textCompare.ContentsEqual("",true);
if (sameText6 == true) {
console.log("Missing checkpoint upload ID.");
input.FileClose();
return;
}
}
else {
var newId = hash.GenerateUuid();
if (hash.LastMethodSuccess !== true) {
console.log("Could not generate upload ID.");
input.FileClose();
return;
}
success = checkpoint.UpdateString("account",config.StringOf("account"));
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.UpdateString("path",resourcePath);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.UpdateString("sha256",fingerprint);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.UpdateString("size",sourceSize);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.UpdateInt("block_size",blockSize);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.UpdateString("upload_id",newId);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = checkpoint.WriteFile("qa_data/azure/upload-checkpoint.tmp");
if (success !== true) {
console.log("Could not write checkpoint.");
input.FileClose();
return;
}
success = files.FileRename("qa_data/azure/upload-checkpoint.tmp",checkpointFile);
if (success !== true) {
console.log("Could not publish checkpoint.");
input.FileClose();
return;
}
}
var uploadId = checkpoint.StringOf("upload_id");
var idCheck = new chilkat.StringBuilder();
success = idCheck.Append(uploadId);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
if (idCheck.Length !== 36) {
console.log("Invalid upload ID length in checkpoint.");
input.FileClose();
return;
}
// Reconcile an uncertain commit before staging anything else.
responseText = rest.FullRequestNoBody("HEAD",resourcePath);
if (rest.LastMethodSuccess !== true) {
console.log("Could not inspect destination.");
input.FileClose();
return;
}
if (rest.ResponseStatusCode == 200) {
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-upload-id"));
var sameText7 = textCompare.ContentsEqual(uploadId,true);
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-source-sha256"));
var sameText8 = textCompare.ContentsEqual(fingerprint,true);
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("Content-Length"));
var sameText9 = textCompare.ContentsEqual(sourceSize,true);
if ((sameText7 !== true) || (sameText8 !== true) || (sameText9 !== true)) {
console.log("Destination exists and is not this completed upload. Refusing to replace it.");
input.FileClose();
return;
}
console.log("Upload already committed. Keep the checkpoint until the application records completion.");
input.FileClose();
return;
}
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"));
var sameText10 = textCompare.ContentsEqual("BlobNotFound",true);
if ((rest.ResponseStatusCode !== 404) || (sameText10 !== true)) {
console.log("Expected an absent blob in an existing container.");
input.FileClose();
return;
}
success = rest.AddQueryParam("comp","blocklist");
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = rest.AddQueryParam("blocklisttype","uncommitted");
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success !== true) {
console.log("TLS reconnection failed.");
input.FileClose();
return;
}
success = rest.UseConnection(connection,false);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
responseText = rest.FullRequestNoBody("GET",resourcePath);
if (rest.LastMethodSuccess !== true) {
console.log("Could not reconcile uploaded blocks.");
input.FileClose();
return;
}
var remoteBlocks = new chilkat.Hashtable();
if (rest.ResponseStatusCode == 200) {
success = result.LoadXml(responseText);
if (success !== true) {
console.log("Expected well-formed XML.");
input.FileClose();
return;
}
if (result.TagEquals("BlockList") !== true) {
console.log("Unexpected XML response root.");
input.FileClose();
return;
}
var remoteCount = result.NumChildrenAt("UncommittedBlocks");
var j = 0;
while ((j < remoteCount)) {
result.I = j;
var remoteId = result.GetChildContent("UncommittedBlocks|Block[i]|Name");
var remoteSize = result.GetChildContent("UncommittedBlocks|Block[i]|Size");
textCompare.Clear();
textCompare.SetString(remoteId);
var sameText11 = textCompare.ContentsEqual("",true);
textCompare.Clear();
textCompare.SetString(remoteSize);
var sameText12 = textCompare.ContentsEqual("",true);
if ((sameText11 == true) || (sameText12 == true)) {
console.log("Malformed remote block listing.");
input.FileClose();
return;
}
success = remoteBlocks.AddStr(remoteId,remoteSize);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
j = j+1;
}
}
else {
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"));
var sameText13 = textCompare.ContentsEqual("BlobNotFound",true);
if ((rest.ResponseStatusCode !== 404) || (sameText13 !== true)) {
console.log("Block listing failed.");
input.FileClose();
return;
}
}
var blocks = new chilkat.Xml();
blocks.Tag = "BlockList";
var chunk = new chilkat.BinData();
var blockText = new chilkat.StringBuilder();
var sizeText = new chilkat.StringBuilder();
var i = 0;
while ((i < numBlocks)) {
success = chunk.Clear();
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = input.ReadBlockBd(i,blockSize,chunk);
if (success !== true) {
console.log("Could not read source block.");
input.FileClose();
return;
}
if ((chunk.NumBytes < 1) || ((i < numBlocks - 1) && (chunk.NumBytes !== blockSize))) {
console.log("Source changed or an incomplete read occurred.");
input.FileClose();
return;
}
blockText.Clear();
success = blockText.Append(uploadId);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = blockText.Append(input.GenBlockId(i,5,"ascii"));
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = blockText.Encode("base64","utf-8");
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
var blockId = blockText.GetAsString();
sizeText.Clear();
success = sizeText.AppendInt(chunk.NumBytes);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
var uploadBlock = true;
if (remoteBlocks.Contains(blockId) == true) {
if (remoteBlocks.LookupStr(blockId) !== sizeText.GetAsString()) {
console.log("Remote block size differs; do not commit this upload.");
input.FileClose();
return;
}
uploadBlock = false;
}
if (uploadBlock == true) {
success = rest.ClearAllQueryParams();
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = rest.AddQueryParam("comp","block");
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
success = rest.AddQueryParam("blockid",blockId);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success !== true) {
console.log("TLS reconnection failed.");
input.FileClose();
return;
}
success = rest.UseConnection(connection,false);
if (success !== true) {
console.log("Could not prepare the request.");
input.FileClose();
return;
}
responseBody.Clear();
success = rest.FullRequestBd("PUT",resourcePath,chunk,responseBody);
if (success !== true) {
console.log("Request did not complete; outcome may be uncertain.");
input.FileClose();
return;
}
if (rest.ResponseStatusCode !== 201) {
console.log("HTTP status: " + rest.ResponseStatusCode);
console.log("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"));
console.log("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
input.FileClose();
return;
}
}
blocks.NewChild2("Uncommitted",blockId);
i = i+1;
}
// Rehash before commit. Do not modify the source or run concurrent writers.
var finalFingerprint = hash.HashFileENC(sourceFile);
textCompare.Clear();
textCompare.SetString(finalFingerprint);
var sameText14 = textCompare.ContentsEqual(fingerprint,true);
if ((hash.LastMethodSuccess !== true) || (sameText14 !== true)) {
console.log("Source changed during staging; commit cancelled.");
input.FileClose();
return;
}
input.FileClose();
success = rest.ClearAllQueryParams();
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddQueryParam("comp","blocklist");
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddHeader("Content-Type","application/xml");
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddHeader("x-ms-blob-content-type","application/octet-stream");
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddHeader("If-None-Match","*");
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddHeader("x-ms-meta-upload-id",uploadId);
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
success = rest.AddHeader("x-ms-meta-source-sha256",fingerprint);
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success !== true) {
console.log("TLS reconnection failed.");
return;
}
success = rest.UseConnection(connection,false);
if (success !== true) {
console.log("Could not prepare the request.");
return;
}
responseText = rest.FullRequestString("PUT",resourcePath,blocks.GetXml());
if (rest.LastMethodSuccess !== true) {
console.log("Request did not complete; outcome may be uncertain.");
return;
}
if (rest.ResponseStatusCode !== 201) {
console.log("HTTP status: " + rest.ResponseStatusCode);
console.log("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"));
console.log("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
return;
}
console.log("Upload committed. Retain the checkpoint for reconciliation until completion is recorded.");
}
chilkatExample();