Sample code for 30+ languages & platforms
C# Requires Chilkat v11.6.0+

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.

Microsoft reference.

Chilkat C# Downloads

C#
bool success = false;

//  StringBuilder compares text by value in every generated language.
Chilkat.StringBuilder textCompare = new Chilkat.StringBuilder();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
    Debug.WriteLine("Could not load private Azure configuration.");
    return;
}

textCompare.Clear();
textCompare.SetString(config.StringOf("account"));
bool sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
    Debug.WriteLine("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"));
bool sameText2 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
    Debug.WriteLine("Supply the base64 account access key.");
    return;
}

Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
success = endpoint.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = endpoint.Append(".blob.core.windows.net");
if (success != true) {
    Debug.WriteLine("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.
string resourcePath = "/examplecontainer/example.bin";
Chilkat.Rest rest = new Chilkat.Rest();
rest.ConnectTimeoutMs = 20000;
rest.IdleTimeoutMs = 60000;
Chilkat.AuthAzureStorage 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) {
    Debug.WriteLine("Could not configure Shared Key signing.");
    return;
}

Chilkat.Socket connection = new Chilkat.Socket();
connection.RequireSslCertVerify = true;
connection.MaxReadIdleMs = 60000;
connection.MaxSendIdleMs = 60000;
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("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) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

string responseText;
Chilkat.StringBuilder responseBody = new Chilkat.StringBuilder();
Chilkat.Xml result = new Chilkat.Xml();
result.Strict = true;
string sourceFile = "qa_data/bin/sample.bin";
string checkpointFile = "qa_data/azure/upload-checkpoint.json";
Chilkat.FileAccess input = new Chilkat.FileAccess();
success = input.OpenForRead(sourceFile);
if (success != true) {
    Debug.WriteLine("Could not open upload source.");
    input.FileClose();
    return;
}

string sourceSize = input.FileSizeStr(sourceFile);
if (input.LastMethodSuccess != true) {
    Debug.WriteLine("Could not read source size.");
    input.FileClose();
    return;
}

int blockSize = 4194304;
int numBlocks = input.GetNumBlocks(blockSize);
if ((input.LastMethodSuccess != true) || (numBlocks < 0) || (numBlocks > 50000)) {
    Debug.WriteLine("Source exceeds the 50,000-block demonstration bound.");
    input.FileClose();
    return;
}

Chilkat.Crypt2 hash = new Chilkat.Crypt2();
hash.HashAlgorithm = "sha256";
hash.EncodingMode = "hex_lower";
string fingerprint = hash.HashFileENC(sourceFile);
if (hash.LastMethodSuccess != true) {
    Debug.WriteLine("Could not fingerprint source.");
    input.FileClose();
    return;
}

Chilkat.JsonObject checkpoint = new Chilkat.JsonObject();
checkpoint.Strict = true;
Chilkat.FileAccess files = new Chilkat.FileAccess();
if (files.FileExists(checkpointFile) == true) {
    success = checkpoint.LoadFile(checkpointFile);
    if (success != true) {
        Debug.WriteLine("Invalid checkpoint; do not silently start another upload.");
        input.FileClose();
        return;
    }

    textCompare.Clear();
    textCompare.SetString(checkpoint.StringOf("path"));
    bool sameText3 = textCompare.ContentsEqual(resourcePath,true);
    textCompare.Clear();
    textCompare.SetString(checkpoint.StringOf("sha256"));
    bool sameText4 = textCompare.ContentsEqual(fingerprint,true);
    textCompare.Clear();
    textCompare.SetString(checkpoint.StringOf("size"));
    bool sameText5 = textCompare.ContentsEqual(sourceSize,true);
    if ((checkpoint.StringOf("account") != config.StringOf("account")) || (sameText3 != true) || (sameText4 != true) || (sameText5 != true) || (checkpoint.IntOf("block_size") != blockSize)) {
        Debug.WriteLine("Checkpoint target or source changed. Choose a new checkpoint/target deliberately.");
        input.FileClose();
        return;
    }

    textCompare.Clear();
    textCompare.SetString(checkpoint.StringOf("upload_id"));
    bool sameText6 = textCompare.ContentsEqual("",true);
    if (sameText6 == true) {
        Debug.WriteLine("Missing checkpoint upload ID.");
        input.FileClose();
        return;
    }

}
else {
    string newId = hash.GenerateUuid();
    if (hash.LastMethodSuccess != true) {
        Debug.WriteLine("Could not generate upload ID.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateString("account",config.StringOf("account"));
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateString("path",resourcePath);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateString("sha256",fingerprint);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateString("size",sourceSize);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateInt("block_size",blockSize);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.UpdateString("upload_id",newId);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = checkpoint.WriteFile("qa_data/azure/upload-checkpoint.tmp");
    if (success != true) {
        Debug.WriteLine("Could not write checkpoint.");
        input.FileClose();
        return;
    }

    success = files.FileRename("qa_data/azure/upload-checkpoint.tmp",checkpointFile);
    if (success != true) {
        Debug.WriteLine("Could not publish checkpoint.");
        input.FileClose();
        return;
    }

}

string uploadId = checkpoint.StringOf("upload_id");
Chilkat.StringBuilder idCheck = new Chilkat.StringBuilder();
success = idCheck.Append(uploadId);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    input.FileClose();
    return;
}

if (idCheck.Length != 36) {
    Debug.WriteLine("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) {
    Debug.WriteLine("Could not inspect destination.");
    input.FileClose();
    return;
}

if (rest.ResponseStatusCode == 200) {
    textCompare.Clear();
    textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-upload-id"));
    bool sameText7 = textCompare.ContentsEqual(uploadId,true);
    textCompare.Clear();
    textCompare.SetString(rest.ResponseHdrByName("x-ms-meta-source-sha256"));
    bool sameText8 = textCompare.ContentsEqual(fingerprint,true);
    textCompare.Clear();
    textCompare.SetString(rest.ResponseHdrByName("Content-Length"));
    bool sameText9 = textCompare.ContentsEqual(sourceSize,true);
    if ((sameText7 != true) || (sameText8 != true) || (sameText9 != true)) {
        Debug.WriteLine("Destination exists and is not this completed upload. Refusing to replace it.");
        input.FileClose();
        return;
    }

    Debug.WriteLine("Upload already committed. Keep the checkpoint until the application records completion.");
    input.FileClose();
    return;
}

textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"));
bool sameText10 = textCompare.ContentsEqual("BlobNotFound",true);
if ((rest.ResponseStatusCode != 404) || (sameText10 != true)) {
    Debug.WriteLine("Expected an absent blob in an existing container.");
    input.FileClose();
    return;
}

success = rest.AddQueryParam("comp","blocklist");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    input.FileClose();
    return;
}

success = rest.AddQueryParam("blocklisttype","uncommitted");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    input.FileClose();
    return;
}

rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS reconnection failed.");
    input.FileClose();
    return;
}

success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    input.FileClose();
    return;
}

responseText = rest.FullRequestNoBody("GET",resourcePath);
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Could not reconcile uploaded blocks.");
    input.FileClose();
    return;
}

Chilkat.Hashtable remoteBlocks = new Chilkat.Hashtable();
if (rest.ResponseStatusCode == 200) {
    success = result.LoadXml(responseText);
    if (success != true) {
        Debug.WriteLine("Expected well-formed XML.");
        input.FileClose();
        return;
    }

    if (result.TagEquals("BlockList") != true) {
        Debug.WriteLine("Unexpected XML response root.");
        input.FileClose();
        return;
    }

    int remoteCount = result.NumChildrenAt("UncommittedBlocks");
    int j = 0;
    while ((j < remoteCount)) {
        result.I = j;
        string remoteId = result.GetChildContent("UncommittedBlocks|Block[i]|Name");
        string remoteSize = result.GetChildContent("UncommittedBlocks|Block[i]|Size");
        textCompare.Clear();
        textCompare.SetString(remoteId);
        bool sameText11 = textCompare.ContentsEqual("",true);
        textCompare.Clear();
        textCompare.SetString(remoteSize);
        bool sameText12 = textCompare.ContentsEqual("",true);
        if ((sameText11 == true) || (sameText12 == true)) {
            Debug.WriteLine("Malformed remote block listing.");
            input.FileClose();
            return;
        }

        success = remoteBlocks.AddStr(remoteId,remoteSize);
        if (success != true) {
            Debug.WriteLine("Could not prepare the request.");
            input.FileClose();
            return;
        }

        j = j + 1;
    }

}
else {
    textCompare.Clear();
    textCompare.SetString(rest.ResponseHdrByName("x-ms-error-code"));
    bool sameText13 = textCompare.ContentsEqual("BlobNotFound",true);
    if ((rest.ResponseStatusCode != 404) || (sameText13 != true)) {
        Debug.WriteLine("Block listing failed.");
        input.FileClose();
        return;
    }

}

Chilkat.Xml blocks = new Chilkat.Xml();
blocks.Tag = "BlockList";
Chilkat.BinData chunk = new Chilkat.BinData();
Chilkat.StringBuilder blockText = new Chilkat.StringBuilder();
Chilkat.StringBuilder sizeText = new Chilkat.StringBuilder();
int i = 0;
while ((i < numBlocks)) {
    success = chunk.Clear();
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = input.ReadBlockBd(i,blockSize,chunk);
    if (success != true) {
        Debug.WriteLine("Could not read source block.");
        input.FileClose();
        return;
    }

    if ((chunk.NumBytes < 1) || ((i < numBlocks - 1) && (chunk.NumBytes != blockSize))) {
        Debug.WriteLine("Source changed or an incomplete read occurred.");
        input.FileClose();
        return;
    }

    blockText.Clear();
    success = blockText.Append(uploadId);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = blockText.Append(input.GenBlockId(i,5,"ascii"));
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    success = blockText.Encode("base64","utf-8");
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    string blockId = blockText.GetAsString();
    sizeText.Clear();
    success = sizeText.AppendInt(chunk.NumBytes);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        input.FileClose();
        return;
    }

    bool uploadBlock = true;
    if (remoteBlocks.Contains(blockId) == true) {
        if (remoteBlocks.LookupStr(blockId) != sizeText.GetAsString()) {
            Debug.WriteLine("Remote block size differs; do not commit this upload.");
            input.FileClose();
            return;
        }

        uploadBlock = false;
    }

    if (uploadBlock == true) {
        success = rest.ClearAllQueryParams();
        if (success != true) {
            Debug.WriteLine("Could not prepare the request.");
            input.FileClose();
            return;
        }

        success = rest.AddQueryParam("comp","block");
        if (success != true) {
            Debug.WriteLine("Could not prepare the request.");
            input.FileClose();
            return;
        }

        success = rest.AddQueryParam("blockid",blockId);
        if (success != true) {
            Debug.WriteLine("Could not prepare the request.");
            input.FileClose();
            return;
        }

        rest.Disconnect(10);
        success = connection.Connect(endpoint.GetAsString(),443,true,20000);
        if (success != true) {
            Debug.WriteLine("TLS reconnection failed.");
            input.FileClose();
            return;
        }

        success = rest.UseConnection(connection,false);
        if (success != true) {
            Debug.WriteLine("Could not prepare the request.");
            input.FileClose();
            return;
        }

        responseBody.Clear();
        success = rest.FullRequestBd("PUT",resourcePath,chunk,responseBody);
        if (success != true) {
            Debug.WriteLine("Request did not complete; outcome may be uncertain.");
            input.FileClose();
            return;
        }

        if (rest.ResponseStatusCode != 201) {
            Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
            Debug.WriteLine("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"));
            Debug.WriteLine("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.
string finalFingerprint = hash.HashFileENC(sourceFile);
textCompare.Clear();
textCompare.SetString(finalFingerprint);
bool sameText14 = textCompare.ContentsEqual(fingerprint,true);
if ((hash.LastMethodSuccess != true) || (sameText14 != true)) {
    Debug.WriteLine("Source changed during staging; commit cancelled.");
    input.FileClose();
    return;
}

input.FileClose();
success = rest.ClearAllQueryParams();
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddQueryParam("comp","blocklist");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Content-Type","application/xml");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("x-ms-blob-content-type","application/octet-stream");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("If-None-Match","*");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("x-ms-meta-upload-id",uploadId);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("x-ms-meta-source-sha256",fingerprint);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS reconnection failed.");
    return;
}

success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

responseText = rest.FullRequestString("PUT",resourcePath,blocks.GetXml());
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; outcome may be uncertain.");
    return;
}

if (rest.ResponseStatusCode != 201) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    return;
}

Debug.WriteLine("Upload committed. Retain the checkpoint for reconciliation until completion is recorded.");