Azure Blob: List Versions and Recover Deleted Content
See more Azure Cloud Storage Examples
For a flat-namespace account, list the versions, snapshots and soft-deleted entries for one blob. Keep version IDs and snapshot timestamps as strings. The default action only lists choices; change action deliberately to undelete, restore-version or restore-snapshot and supply the selected ID.
Undelete recovers eligible soft-deleted content within its retention period. When versioning is enabled, undelete does not automatically make an older version the current base blob: restore the selected live version by copying it to the base name. A deleted snapshot/version may first require undelete. Restoring over an existing base requires its current quoted ETag; otherwise this example only creates an absent base. Retention expiration, permanent deletion and immutability policies can prevent recovery. Hierarchical-namespace accounts use a different recovery workflow with deletion IDs.
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;
result: TChilkatXml;
action: WideString;
selectedId: WideString;
destinationEtag: WideString;
seenMarkers: TChilkatHashtable;
marker: WideString;
more: Integer;
page: Integer;
count: Integer;
i: Integer;
sameText3: Integer;
sameText4: Integer;
sameText5: Integer;
sameText6: Integer;
sameText7: Integer;
sameText8: Integer;
sameText9: Integer;
encodedId: TChilkatStringBuilder;
restoreUrl: TChilkatStringBuilder;
sameText10: Integer;
sameText11: Integer;
copyId: WideString;
sameText12: Integer;
polls: Integer;
completed: Integer;
sameText13: Integer;
copyStatus: WideString;
sameText14: Integer;
sameText15: 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';
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;
result := TChilkatXml.Create(Self);
result.Strict := 1;
// Flat-namespace Blob account. Default is inspection only.
// Choose list, undelete, restore-version, or restore-snapshot.
action := 'list';
selectedId := '';
// For restoring over an existing base blob, supply its current quoted ETag.
// Empty means restore only when the base blob is absent.
destinationEtag := '';
success := rest.AddQueryParam('restype','container');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('comp','list');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('prefix','example.bin');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('include','versions,snapshots,deleted,deletedwithversions');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('maxresults','1000');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
seenMarkers := TChilkatHashtable.Create(Self);
marker := '';
more := 1;
page := 0;
while (more = 1) do
begin
if (page >= 1000) then
begin
Memo1.Lines.Add('Page limit reached; results are incomplete. Save the continuation marker.');
Exit;
end;
responseText := rest.FullRequestNoBody('GET',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (rest.ResponseStatusCode <> 200) 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;
success := result.LoadXml(responseText);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected well-formed XML.');
Exit;
end;
if (result.TagEquals('EnumerationResults') <> 1) then
begin
Memo1.Lines.Add('Unexpected XML response root.');
Exit;
end;
count := result.NumChildrenAt('Blobs');
i := 0;
while (i < count) do
begin
result.I := i;
textCompare.Clear();
textCompare.SetString(result.GetChildContent('Blobs|Blob[i]|Name'));
sameText3 := textCompare.ContentsEqual('example.bin',1);
if (sameText3 = 1) then
begin
Memo1.Lines.Add('Version ID: ' + result.GetChildContent('Blobs|Blob[i]|VersionId'));
Memo1.Lines.Add('Snapshot: ' + result.GetChildContent('Blobs|Blob[i]|Snapshot'));
Memo1.Lines.Add('Deleted: ' + result.GetChildContent('Blobs|Blob[i]|Deleted'));
Memo1.Lines.Add('Current version: ' + result.GetChildContent('Blobs|Blob[i]|IsCurrentVersion'));
Memo1.Lines.Add('Has versions only: ' + result.GetChildContent('Blobs|Blob[i]|HasVersionsOnly'));
end;
i := i + 1;
end;
marker := result.GetChildContent('NextMarker');
page := page + 1;
textCompare.Clear();
textCompare.SetString(marker);
sameText4 := textCompare.ContentsEqual('',1);
if (sameText4 = 1) then
begin
more := 0;
end
else
begin
if (seenMarkers.Contains(marker) = 1) then
begin
Memo1.Lines.Add('Repeated marker; results are incomplete.');
Exit;
end;
success := seenMarkers.AddInt(marker,1);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
if (page > 1) then
begin
success := rest.RemoveQueryParam('marker');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end;
success := rest.AddQueryParam('marker',marker);
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;
end;
end;
textCompare.Clear();
textCompare.SetString(action);
sameText5 := textCompare.ContentsEqual('list',1);
if (sameText5 <> 1) then
begin
success := rest.ClearAllQueryParams();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
resourcePath := '/examplecontainer/example.bin';
textCompare.Clear();
textCompare.SetString(action);
sameText6 := textCompare.ContentsEqual('undelete',1);
if (sameText6 = 1) then
begin
success := rest.AddQueryParam('comp','undelete');
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.FullRequestNoBody('PUT',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (rest.ResponseStatusCode <> 200) 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('Undelete accepted. Rerun listing before selecting a version or snapshot.');
end
else
begin
textCompare.Clear();
textCompare.SetString(action);
sameText7 := textCompare.ContentsEqual('restore-version',1);
textCompare.Clear();
textCompare.SetString(action);
sameText8 := textCompare.ContentsEqual('restore-snapshot',1);
if ((sameText7 <> 1) and (sameText8 <> 1)) then
begin
Memo1.Lines.Add('Unknown recovery action.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(selectedId);
sameText9 := textCompare.ContentsEqual('',1);
if (sameText9 = 1) then
begin
Memo1.Lines.Add('Select a version or snapshot ID from the listing first.');
Exit;
end;
encodedId := TChilkatStringBuilder.Create(Self);
success := encodedId.Append(selectedId);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := encodedId.Encode('url','utf-8');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
restoreUrl := TChilkatStringBuilder.Create(Self);
success := restoreUrl.Append('https://');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := restoreUrl.Append(endpoint.GetAsString());
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := restoreUrl.Append(resourcePath);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(action);
sameText10 := textCompare.ContentsEqual('restore-version',1);
if (sameText10 = 1) then
begin
success := restoreUrl.Append('?versionid=');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end
else
begin
success := restoreUrl.Append('?snapshot=');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end;
success := restoreUrl.Append(encodedId.GetAsString());
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('x-ms-copy-source',restoreUrl.GetAsString());
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(destinationEtag);
sameText11 := textCompare.ContentsEqual('',1);
if (sameText11 = 1) then
begin
success := rest.AddHeader('If-None-Match','*');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end
else
begin
success := rest.AddHeader('If-Match',destinationEtag);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
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.FullRequestNoBody('PUT',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (rest.ResponseStatusCode <> 202) 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;
copyId := rest.ResponseHdrByName('x-ms-copy-id');
textCompare.Clear();
textCompare.SetString(copyId);
sameText12 := textCompare.ContentsEqual('',1);
if (sameText12 = 1) then
begin
Memo1.Lines.Add('Copy accepted without a copy ID; inspect before retrying.');
Exit;
end;
Memo1.Lines.Add('Copy ID: ' + copyId);
// Save copyId and destination in application state if polling must survive restart.
success := rest.ClearAllHeaders();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.ClearAllQueryParams();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
polls := 0;
completed := 0;
while ((polls < 20) and (completed <> 1)) do
begin
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.FullRequestNoBody('HEAD',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (rest.ResponseStatusCode <> 200) 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;
textCompare.Clear();
textCompare.SetString(rest.ResponseHdrByName('x-ms-copy-id'));
sameText13 := textCompare.ContentsEqual(copyId,1);
if (sameText13 <> 1) then
begin
Memo1.Lines.Add('Destination no longer reports this copy ID.');
Exit;
end;
copyStatus := rest.ResponseHdrByName('x-ms-copy-status');
textCompare.Clear();
textCompare.SetString(copyStatus);
sameText14 := textCompare.ContentsEqual('success',1);
if (sameText14 = 1) then
begin
completed := 1;
end
else
begin
textCompare.Clear();
textCompare.SetString(copyStatus);
sameText15 := textCompare.ContentsEqual('pending',1);
if (sameText15 <> 1) then
begin
Memo1.Lines.Add('Copy failed, was aborted, or returned an unexpected status; inspect copy diagnostics privately.');
Exit;
end;
// Pause between polls; the copy can outlast this example's bounded wait.
connection.SleepMs(1000);
end;
polls := polls + 1;
end;
if (completed <> 1) then
begin
Memo1.Lines.Add('Copy is still pending. Continue polling this ID; do not start another copy.');
Exit;
end;
Memo1.Lines.Add('Copy completed.');
end;
end;
Memo1.Lines.Add('Recovery workflow finished.');