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 PHP ActiveX Downloads
<?php
$success = 0;
// StringBuilder compares text by value in every generated language.
$textCompare = new COM("Chilkat.StringBuilder");
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('qa_data/azure/storage.json');
if ($success != 1) {
print 'Could not load private Azure configuration.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->stringOf('account'));
$sameText1 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('account') != 1) or ($sameText1 == 1)) {
print 'Supply the storage account name.' . "\n";
exit;
}
// access_key is the base64 account key, not a connection string or SAS.
$textCompare->Clear();
$textCompare->SetString($config->stringOf('access_key'));
$sameText2 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('access_key') != 1) or ($sameText2 == 1)) {
print 'Supply the base64 account access key.' . "\n";
exit;
}
$endpoint = new COM("Chilkat.StringBuilder");
$success = $endpoint->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $endpoint->Append('.blob.core.windows.net');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
$resourcePath = '/examplecontainer/example.bin';
$rest = new COM("Chilkat.Rest");
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$azAuth = new COM("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 != 1) {
print 'Could not configure Shared Key signing.' . "\n";
exit;
}
$connection = new COM("Chilkat.Socket");
$connection->RequireSslCertVerify = 1;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS connection failed; inspect diagnostics privately.' . "\n";
exit;
}
// Do not replay a write automatically after losing its response.
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$responseBody = new COM("Chilkat.StringBuilder");
$result = new COM("Chilkat.Xml");
$result->Strict = 1;
$sourceFile = 'qa_data/bin/sample.bin';
$checkpointFile = 'qa_data/azure/upload-checkpoint.json';
$input = new COM("Chilkat.FileAccess");
$success = $input->OpenForRead($sourceFile);
if ($success != 1) {
print 'Could not open upload source.' . "\n";
$input->FileClose();
exit;
}
$sourceSize = $input->fileSizeStr($sourceFile);
if ($input->LastMethodSuccess != 1) {
print 'Could not read source size.' . "\n";
$input->FileClose();
exit;
}
$blockSize = 4194304;
$numBlocks = $input->GetNumBlocks($blockSize);
if (($input->LastMethodSuccess != 1) or ($numBlocks < 0) or ($numBlocks > 50000)) {
print 'Source exceeds the 50,000-block demonstration bound.' . "\n";
$input->FileClose();
exit;
}
$hash = new COM("Chilkat.Crypt2");
$hash->HashAlgorithm = 'sha256';
$hash->EncodingMode = 'hex_lower';
$fingerprint = $hash->hashFileENC($sourceFile);
if ($hash->LastMethodSuccess != 1) {
print 'Could not fingerprint source.' . "\n";
$input->FileClose();
exit;
}
$checkpoint = new COM("Chilkat.JsonObject");
$checkpoint->Strict = 1;
$files = new COM("Chilkat.FileAccess");
if ($files->FileExists($checkpointFile) == 1) {
$success = $checkpoint->LoadFile($checkpointFile);
if ($success != 1) {
print 'Invalid checkpoint; do not silently start another upload.' . "\n";
$input->FileClose();
exit;
}
$textCompare->Clear();
$textCompare->SetString($checkpoint->stringOf('path'));
$sameText3 = $textCompare->ContentsEqual($resourcePath,1);
$textCompare->Clear();
$textCompare->SetString($checkpoint->stringOf('sha256'));
$sameText4 = $textCompare->ContentsEqual($fingerprint,1);
$textCompare->Clear();
$textCompare->SetString($checkpoint->stringOf('size'));
$sameText5 = $textCompare->ContentsEqual($sourceSize,1);
if (($checkpoint->stringOf('account') != $config->stringOf('account')) or ($sameText3 != 1) or ($sameText4 != 1) or ($sameText5 != 1) or ($checkpoint->IntOf('block_size') != $blockSize)) {
print 'Checkpoint target or source changed. Choose a new checkpoint/target deliberately.' . "\n";
$input->FileClose();
exit;
}
$textCompare->Clear();
$textCompare->SetString($checkpoint->stringOf('upload_id'));
$sameText6 = $textCompare->ContentsEqual('',1);
if ($sameText6 == 1) {
print 'Missing checkpoint upload ID.' . "\n";
$input->FileClose();
exit;
}
}
else {
$newId = $hash->generateUuid();
if ($hash->LastMethodSuccess != 1) {
print 'Could not generate upload ID.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateString('account',$config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateString('path',$resourcePath);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateString('sha256',$fingerprint);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateString('size',$sourceSize);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateInt('block_size',$blockSize);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->UpdateString('upload_id',$newId);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $checkpoint->WriteFile('qa_data/azure/upload-checkpoint.tmp');
if ($success != 1) {
print 'Could not write checkpoint.' . "\n";
$input->FileClose();
exit;
}
$success = $files->FileRename('qa_data/azure/upload-checkpoint.tmp',$checkpointFile);
if ($success != 1) {
print 'Could not publish checkpoint.' . "\n";
$input->FileClose();
exit;
}
}
$uploadId = $checkpoint->stringOf('upload_id');
$idCheck = new COM("Chilkat.StringBuilder");
$success = $idCheck->Append($uploadId);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
if ($idCheck->Length != 36) {
print 'Invalid upload ID length in checkpoint.' . "\n";
$input->FileClose();
exit;
}
// Reconcile an uncertain commit before staging anything else.
$responseText = $rest->fullRequestNoBody('HEAD',$resourcePath);
if ($rest->LastMethodSuccess != 1) {
print 'Could not inspect destination.' . "\n";
$input->FileClose();
exit;
}
if ($rest->ResponseStatusCode == 200) {
$textCompare->Clear();
$textCompare->SetString($rest->responseHdrByName('x-ms-meta-upload-id'));
$sameText7 = $textCompare->ContentsEqual($uploadId,1);
$textCompare->Clear();
$textCompare->SetString($rest->responseHdrByName('x-ms-meta-source-sha256'));
$sameText8 = $textCompare->ContentsEqual($fingerprint,1);
$textCompare->Clear();
$textCompare->SetString($rest->responseHdrByName('Content-Length'));
$sameText9 = $textCompare->ContentsEqual($sourceSize,1);
if (($sameText7 != 1) or ($sameText8 != 1) or ($sameText9 != 1)) {
print 'Destination exists and is not this completed upload. Refusing to replace it.' . "\n";
$input->FileClose();
exit;
}
print 'Upload already committed. Keep the checkpoint until the application records completion.' . "\n";
$input->FileClose();
exit;
}
$textCompare->Clear();
$textCompare->SetString($rest->responseHdrByName('x-ms-error-code'));
$sameText10 = $textCompare->ContentsEqual('BlobNotFound',1);
if (($rest->ResponseStatusCode != 404) or ($sameText10 != 1)) {
print 'Expected an absent blob in an existing container.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->AddQueryParam('comp','blocklist');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->AddQueryParam('blocklisttype','uncommitted');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$rest->Disconnect(10);
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS reconnection failed.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$responseText = $rest->fullRequestNoBody('GET',$resourcePath);
if ($rest->LastMethodSuccess != 1) {
print 'Could not reconcile uploaded blocks.' . "\n";
$input->FileClose();
exit;
}
$remoteBlocks = new COM("Chilkat.Hashtable");
if ($rest->ResponseStatusCode == 200) {
$success = $result->LoadXml($responseText);
if ($success != 1) {
print 'Expected well-formed XML.' . "\n";
$input->FileClose();
exit;
}
if ($result->TagEquals('BlockList') != 1) {
print 'Unexpected XML response root.' . "\n";
$input->FileClose();
exit;
}
$remoteCount = $result->NumChildrenAt('UncommittedBlocks');
$j = 0;
while (($j < $remoteCount)) {
$result->I = $j;
$remoteId = $result->getChildContent('UncommittedBlocks|Block[i]|Name');
$remoteSize = $result->getChildContent('UncommittedBlocks|Block[i]|Size');
$textCompare->Clear();
$textCompare->SetString($remoteId);
$sameText11 = $textCompare->ContentsEqual('',1);
$textCompare->Clear();
$textCompare->SetString($remoteSize);
$sameText12 = $textCompare->ContentsEqual('',1);
if (($sameText11 == 1) or ($sameText12 == 1)) {
print 'Malformed remote block listing.' . "\n";
$input->FileClose();
exit;
}
$success = $remoteBlocks->AddStr($remoteId,$remoteSize);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$j = $j + 1;
}
}
else {
$textCompare->Clear();
$textCompare->SetString($rest->responseHdrByName('x-ms-error-code'));
$sameText13 = $textCompare->ContentsEqual('BlobNotFound',1);
if (($rest->ResponseStatusCode != 404) or ($sameText13 != 1)) {
print 'Block listing failed.' . "\n";
$input->FileClose();
exit;
}
}
$blocks = new COM("Chilkat.Xml");
$blocks->Tag = 'BlockList';
$chunk = new COM("Chilkat.BinData");
$blockText = new COM("Chilkat.StringBuilder");
$sizeText = new COM("Chilkat.StringBuilder");
$i = 0;
while (($i < $numBlocks)) {
$success = $chunk->Clear();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $input->ReadBlockBd($i,$blockSize,$chunk);
if ($success != 1) {
print 'Could not read source block.' . "\n";
$input->FileClose();
exit;
}
if (($chunk->NumBytes < 1) or (($i < $numBlocks - 1) and ($chunk->NumBytes != $blockSize))) {
print 'Source changed or an incomplete read occurred.' . "\n";
$input->FileClose();
exit;
}
$blockText->Clear();
$success = $blockText->Append($uploadId);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $blockText->Append($input->genBlockId($i,5,'ascii'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $blockText->Encode('base64','utf-8');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$blockId = $blockText->getAsString();
$sizeText->Clear();
$success = $sizeText->AppendInt($chunk->NumBytes);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$uploadBlock = 1;
if ($remoteBlocks->Contains($blockId) == 1) {
if ($remoteBlocks->lookupStr($blockId) != $sizeText->getAsString()) {
print 'Remote block size differs; do not commit this upload.' . "\n";
$input->FileClose();
exit;
}
$uploadBlock = 0;
}
if ($uploadBlock == 1) {
$success = $rest->ClearAllQueryParams();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->AddQueryParam('comp','block');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->AddQueryParam('blockid',$blockId);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$rest->Disconnect(10);
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS reconnection failed.' . "\n";
$input->FileClose();
exit;
}
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
$input->FileClose();
exit;
}
$responseBody->Clear();
$success = $rest->FullRequestBd('PUT',$resourcePath,$chunk,$responseBody);
if ($success != 1) {
print 'Request did not complete; outcome may be uncertain.' . "\n";
$input->FileClose();
exit;
}
if ($rest->ResponseStatusCode != 201) {
print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
print 'Azure error code: ' . $rest->responseHdrByName('x-ms-error-code') . "\n";
print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
$input->FileClose();
exit;
}
}
$blocks->NewChild2('Uncommitted',$blockId);
$i = $i + 1;
}
// Rehash before commit. Do not modify the source or run concurrent writers.
$finalFingerprint = $hash->hashFileENC($sourceFile);
$textCompare->Clear();
$textCompare->SetString($finalFingerprint);
$sameText14 = $textCompare->ContentsEqual($fingerprint,1);
if (($hash->LastMethodSuccess != 1) or ($sameText14 != 1)) {
print 'Source changed during staging; commit cancelled.' . "\n";
$input->FileClose();
exit;
}
$input->FileClose();
$success = $rest->ClearAllQueryParams();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddQueryParam('comp','blocklist');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Content-Type','application/xml');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('x-ms-blob-content-type','application/octet-stream');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('If-None-Match','*');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('x-ms-meta-upload-id',$uploadId);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('x-ms-meta-source-sha256',$fingerprint);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$rest->Disconnect(10);
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS reconnection failed.' . "\n";
exit;
}
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$responseText = $rest->fullRequestString('PUT',$resourcePath,$blocks->getXml());
if ($rest->LastMethodSuccess != 1) {
print 'Request did not complete; outcome may be uncertain.' . "\n";
exit;
}
if ($rest->ResponseStatusCode != 201) {
print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
print 'Azure error code: ' . $rest->responseHdrByName('x-ms-error-code') . "\n";
print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
exit;
}
print 'Upload committed. Retain the checkpoint for reconciliation until completion is recorded.' . "\n";
?>