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 Delphi ActiveX Downloads
var
success: Integer;
textCompare: TChilkatStringBuilder;
config: TChilkatJsonObject;
sameText1: Integer;
sameText2: Integer;
endpoint: TChilkatStringBuilder;
resourcePath: WideString;
rest: TChilkatRest;
azAuth: TChilkatAuthAzureStorage;
connection: TChilkatSocket;
responseText: WideString;
responseBody: TChilkatStringBuilder;
result: TChilkatXml;
sourceFile: WideString;
checkpointFile: WideString;
input: TCkFileAccess;
sourceSize: WideString;
blockSize: Integer;
numBlocks: Integer;
hash: TChilkatCrypt2;
fingerprint: WideString;
checkpoint: TChilkatJsonObject;
files: TCkFileAccess;
sameText3: Integer;
sameText4: Integer;
sameText5: Integer;
sameText6: Integer;
newId: WideString;
uploadId: WideString;
idCheck: TChilkatStringBuilder;
sameText7: Integer;
sameText8: Integer;
sameText9: Integer;
sameText10: Integer;
remoteBlocks: TChilkatHashtable;
remoteCount: Integer;
j: Integer;
remoteId: WideString;
remoteSize: WideString;
sameText11: Integer;
sameText12: Integer;
sameText13: Integer;
blocks: TChilkatXml;
chunk: TChilkatBinData;
blockText: TChilkatStringBuilder;
sizeText: TChilkatStringBuilder;
i: Integer;
blockId: WideString;
uploadBlock: Integer;
finalFingerprint: WideString;
sameText14: Integer;
begin
success := 0;
// StringBuilder compares text by value in every generated language.
textCompare := TChilkatStringBuilder.Create(Self);
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
config := TChilkatJsonObject.Create(Self);
config.Strict := 1;
success := config.LoadFile('qa_data/azure/storage.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load private Azure configuration.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(config.StringOf('account'));
sameText1 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('account') <> 1) or (sameText1 = 1)) then
begin
Memo1.Lines.Add('Supply the storage account name.');
Exit;
end;
// 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)) then
begin
Memo1.Lines.Add('Supply the base64 account access key.');
Exit;
end;
endpoint := TChilkatStringBuilder.Create(Self);
success := endpoint.Append(config.StringOf('account'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := endpoint.Append('.blob.core.windows.net');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
// 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 := TChilkatRest.Create(Self);
rest.ConnectTimeoutMs := 20000;
rest.IdleTimeoutMs := 60000;
azAuth := TChilkatAuthAzureStorage.Create(Self);
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.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not configure Shared Key signing.');
Exit;
end;
connection := TChilkatSocket.Create(Self);
connection.RequireSslCertVerify := 1;
connection.MaxReadIdleMs := 60000;
connection.MaxSendIdleMs := 60000;
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
// Do not replay a write automatically after losing its response.
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
responseBody := TChilkatStringBuilder.Create(Self);
result := TChilkatXml.Create(Self);
result.Strict := 1;
sourceFile := 'qa_data/bin/sample.bin';
checkpointFile := 'qa_data/azure/upload-checkpoint.json';
input := TCkFileAccess.Create(Self);
success := input.OpenForRead(sourceFile);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not open upload source.');
input.FileClose();
Exit;
end;
sourceSize := input.FileSizeStr(sourceFile);
if (input.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not read source size.');
input.FileClose();
Exit;
end;
blockSize := 4194304;
numBlocks := input.GetNumBlocks(blockSize);
if ((input.LastMethodSuccess <> 1) or (numBlocks < 0) or (numBlocks > 50000)) then
begin
Memo1.Lines.Add('Source exceeds the 50,000-block demonstration bound.');
input.FileClose();
Exit;
end;
hash := TChilkatCrypt2.Create(Self);
hash.HashAlgorithm := 'sha256';
hash.EncodingMode := 'hex_lower';
fingerprint := hash.HashFileENC(sourceFile);
if (hash.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not fingerprint source.');
input.FileClose();
Exit;
end;
checkpoint := TChilkatJsonObject.Create(Self);
checkpoint.Strict := 1;
files := TCkFileAccess.Create(Self);
if (files.FileExists(checkpointFile) = 1) then
begin
success := checkpoint.LoadFile(checkpointFile);
if (success <> 1) then
begin
Memo1.Lines.Add('Invalid checkpoint; do not silently start another upload.');
input.FileClose();
Exit;
end;
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)) then
begin
Memo1.Lines.Add('Checkpoint target or source changed. Choose a new checkpoint/target deliberately.');
input.FileClose();
Exit;
end;
textCompare.Clear();
textCompare.SetString(checkpoint.StringOf('upload_id'));
sameText6 := textCompare.ContentsEqual('',1);
if (sameText6 = 1) then
begin
Memo1.Lines.Add('Missing checkpoint upload ID.');
input.FileClose();
Exit;
end;
end
else
begin
newId := hash.GenerateUuid();
if (hash.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not generate upload ID.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateString('account',config.StringOf('account'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateString('path',resourcePath);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateString('sha256',fingerprint);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateString('size',sourceSize);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateInt('block_size',blockSize);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.UpdateString('upload_id',newId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := checkpoint.WriteFile('qa_data/azure/upload-checkpoint.tmp');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not write checkpoint.');
input.FileClose();
Exit;
end;
success := files.FileRename('qa_data/azure/upload-checkpoint.tmp',checkpointFile);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not publish checkpoint.');
input.FileClose();
Exit;
end;
end;
uploadId := checkpoint.StringOf('upload_id');
idCheck := TChilkatStringBuilder.Create(Self);
success := idCheck.Append(uploadId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
if (idCheck.Length <> 36) then
begin
Memo1.Lines.Add('Invalid upload ID length in checkpoint.');
input.FileClose();
Exit;
end;
// Reconcile an uncertain commit before staging anything else.
responseText := rest.FullRequestNoBody('HEAD',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not inspect destination.');
input.FileClose();
Exit;
end;
if (rest.ResponseStatusCode = 200) then
begin
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)) then
begin
Memo1.Lines.Add('Destination exists and is not this completed upload. Refusing to replace it.');
input.FileClose();
Exit;
end;
Memo1.Lines.Add('Upload already committed. Keep the checkpoint until the application records completion.');
input.FileClose();
Exit;
end;
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName('x-ms-error-code'));
sameText10 := textCompare.ContentsEqual('BlobNotFound',1);
if ((rest.ResponseStatusCode <> 404) or (sameText10 <> 1)) then
begin
Memo1.Lines.Add('Expected an absent blob in an existing container.');
input.FileClose();
Exit;
end;
success := rest.AddQueryParam('comp','blocklist');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := rest.AddQueryParam('blocklisttype','uncommitted');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
rest.Disconnect(10);
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
begin
Memo1.Lines.Add('TLS reconnection failed.');
input.FileClose();
Exit;
end;
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
responseText := rest.FullRequestNoBody('GET',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not reconcile uploaded blocks.');
input.FileClose();
Exit;
end;
remoteBlocks := TChilkatHashtable.Create(Self);
if (rest.ResponseStatusCode = 200) then
begin
success := result.LoadXml(responseText);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected well-formed XML.');
input.FileClose();
Exit;
end;
if (result.TagEquals('BlockList') <> 1) then
begin
Memo1.Lines.Add('Unexpected XML response root.');
input.FileClose();
Exit;
end;
remoteCount := result.NumChildrenAt('UncommittedBlocks');
j := 0;
while (j < remoteCount) do
begin
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)) then
begin
Memo1.Lines.Add('Malformed remote block listing.');
input.FileClose();
Exit;
end;
success := remoteBlocks.AddStr(remoteId,remoteSize);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
j := j + 1;
end;
end
else
begin
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName('x-ms-error-code'));
sameText13 := textCompare.ContentsEqual('BlobNotFound',1);
if ((rest.ResponseStatusCode <> 404) or (sameText13 <> 1)) then
begin
Memo1.Lines.Add('Block listing failed.');
input.FileClose();
Exit;
end;
end;
blocks := TChilkatXml.Create(Self);
blocks.Tag := 'BlockList';
chunk := TChilkatBinData.Create(Self);
blockText := TChilkatStringBuilder.Create(Self);
sizeText := TChilkatStringBuilder.Create(Self);
i := 0;
while (i < numBlocks) do
begin
success := chunk.Clear();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := input.ReadBlockBd(i,blockSize,chunk.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not read source block.');
input.FileClose();
Exit;
end;
if ((chunk.NumBytes < 1) or ((i < numBlocks - 1) and (chunk.NumBytes <> blockSize))) then
begin
Memo1.Lines.Add('Source changed or an incomplete read occurred.');
input.FileClose();
Exit;
end;
blockText.Clear();
success := blockText.Append(uploadId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := blockText.Append(input.GenBlockId(i,5,'ascii'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := blockText.Encode('base64','utf-8');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
blockId := blockText.GetAsString();
sizeText.Clear();
success := sizeText.AppendInt(chunk.NumBytes);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
uploadBlock := 1;
if (remoteBlocks.Contains(blockId) = 1) then
begin
if (remoteBlocks.LookupStr(blockId) <> sizeText.GetAsString()) then
begin
Memo1.Lines.Add('Remote block size differs; do not commit this upload.');
input.FileClose();
Exit;
end;
uploadBlock := 0;
end;
if (uploadBlock = 1) then
begin
success := rest.ClearAllQueryParams();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := rest.AddQueryParam('comp','block');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
success := rest.AddQueryParam('blockid',blockId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
rest.Disconnect(10);
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
begin
Memo1.Lines.Add('TLS reconnection failed.');
input.FileClose();
Exit;
end;
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
input.FileClose();
Exit;
end;
responseBody.Clear();
success := rest.FullRequestBd('PUT',resourcePath,chunk.ControlInterface,responseBody.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
input.FileClose();
Exit;
end;
if (rest.ResponseStatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(rest.ResponseStatusCode));
Memo1.Lines.Add('Azure error code: ' + rest.ResponseHdrByName('x-ms-error-code'));
Memo1.Lines.Add('Request ID: ' + rest.ResponseHdrByName('x-ms-request-id'));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
input.FileClose();
Exit;
end;
end;
blocks.NewChild2('Uncommitted',blockId);
i := i + 1;
end;
// 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)) then
begin
Memo1.Lines.Add('Source changed during staging; commit cancelled.');
input.FileClose();
Exit;
end;
input.FileClose();
success := rest.ClearAllQueryParams();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('comp','blocklist');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('Content-Type','application/xml');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('x-ms-blob-content-type','application/octet-stream');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('If-None-Match','*');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('x-ms-meta-upload-id',uploadId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('x-ms-meta-source-sha256',fingerprint);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
rest.Disconnect(10);
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
begin
Memo1.Lines.Add('TLS reconnection failed.');
Exit;
end;
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
responseText := rest.FullRequestString('PUT',resourcePath,blocks.GetXml());
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (rest.ResponseStatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(rest.ResponseStatusCode));
Memo1.Lines.Add('Azure error code: ' + rest.ResponseHdrByName('x-ms-error-code'));
Memo1.Lines.Add('Request ID: ' + rest.ResponseHdrByName('x-ms-request-id'));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
Exit;
end;
Memo1.Lines.Add('Upload committed. Retain the checkpoint for reconciliation until completion is recorded.');