Azure Storage Accounts: Configure Blob Soft Delete and Versioning
See more Azure Storage Accounts Examples
Enables 14-day blob/container soft delete and blob versioning through ARM. The example checks for standard StorageV2 without hierarchical namespace, reads existing Blob service properties, and retains unrelated settings such as CORS. It refuses to shorten soft-delete retention below what an existing point-in-time restore policy requires.
Review retention, feature compatibility, existing policies, and storage costs before running. Versioning protects subsequent changes; it does not reconstruct overwritten historical data. Soft delete and versioning do not protect against deleting the entire storage account. Serialize competing service-configuration writers. See blob versioning.
These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.
Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.
Chilkat Delphi DLL Downloads
var
success: Boolean;
token: HCkJsonObject;
subscriptionId: PWideChar;
http: HCkHttp;
response: HCkHttpResponse;
result: HCkJsonObject;
parsed: HCkUrl;
resourceGroupName: PWideChar;
accountName: PWideChar;
resourceUrl: PWideChar;
blobUrl: PWideChar;
properties: HCkJsonObject;
restore: HCkJsonObject;
accessTracking: HCkJsonObject;
request: HCkJsonObject;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global.UnlockBundle for sample code.
// This is Azure Resource Manager (the management plane), not a blob data request.
// Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
// For the v2 token endpoint, request scope https://management.azure.com/.default.
// Keep the token file private and out of source control and public web directories.
token := CkJsonObject_Create();
CkJsonObject_putStrict(token,True);
success := CkJsonObject_LoadFile(token,'qa_data/tokens/azureToken.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load the private Azure Resource Manager token file.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
begin
Memo1.Lines.Add('A nonempty access_token string is required.');
Exit;
end;
// Use your subscription ID as a string. The all-zero value is only a placeholder.
subscriptionId := '00000000-0000-0000-0000-000000000000';
http := CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject__stringOf(token,'access_token'));
CkHttp_putAccept(http,'application/json');
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,30);
CkHttp_putReadTimeout(http,60);
success := CkHttp_SetUrlVar(http,'subscriptionId',subscriptionId);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode subscription ID.');
Exit;
end;
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
parsed := CkUrl_Create();
resourceGroupName := 'my-resource-group';
success := CkHttp_SetUrlVar(http,'resourceGroupName',resourceGroupName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode resource group.');
Exit;
end;
accountName := 'youruniquestorageaccount';
success := CkHttp_SetUrlVar(http,'accountName',accountName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not encode account name.');
Exit;
end;
resourceUrl := 'https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01';
success := CkHttp_HttpNoBody(http,'GET',resourceUrl,response);
if (CkHttpResponse_getStatusCode(response) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect LastErrorText privately; it may contain credentials.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(response) <> 200)) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(response,'Retry-After'));
// Inspect the error body privately. Do not log tokens, headers, or full responses.
// For 401 check the token/audience; for 403 check Azure RBAC and policy.
// Honor Retry-After for 429 and use bounded backoff for transient failures.
Exit;
end;
if (success <> True) then
begin
Memo1.Lines.Add('The HTTP response could not be read completely.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
// This demonstration targets standard StorageV2 without hierarchical namespace.
if ((CkJsonObject_StringOfEquals(result,'kind','StorageV2',True) <> True) or (CkJsonObject_StringOfEquals(result,'sku.tier','Standard',True) <> True) or (CkJsonObject_BoolOf(result,'properties.isHnsEnabled') = True)) then
begin
Memo1.Lines.Add('This versioning example requires standard StorageV2 without hierarchical namespace.');
Exit;
end;
blobUrl := 'https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/blobServices/default?api-version=2026-09-01';
success := CkHttp_HttpNoBody(http,'GET',blobUrl,response);
if (CkHttpResponse_getStatusCode(response) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect LastErrorText privately; it may contain credentials.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(response) <> 200)) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(response,'Retry-After'));
// Inspect the error body privately. Do not log tokens, headers, or full responses.
// For 401 check the token/audience; for 403 check Azure RBAC and policy.
// Honor Retry-After for 429 and use bounded backoff for transient failures.
Exit;
end;
if (success <> True) then
begin
Memo1.Lines.Add('The HTTP response could not be read completely.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(result,'properties') <> 3) then
begin
Memo1.Lines.Add('Missing or invalid properties.');
Exit;
end;
properties := CkJsonObject_Create();
success := CkJsonObject_ObjectOf2(result,'properties',properties);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read blob-service properties.');
Exit;
end;
// Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
CkJsonObject_Delete(properties,'automaticSnapshotPolicyEnabled');
restore := CkJsonObject_Create();
if (CkJsonObject_JsonTypeOf(properties,'restorePolicy') = 3) then
begin
success := CkJsonObject_ObjectOf2(properties,'restorePolicy',restore);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read restore policy.');
Exit;
end;
CkJsonObject_Delete(restore,'lastEnabledTime');
CkJsonObject_Delete(restore,'minRestoreTime');
if ((CkJsonObject_BoolOf(restore,'enabled') = True) and (CkJsonObject_IntOf(restore,'days') >= 14)) then
begin
Memo1.Lines.Add('Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately.');
Exit;
end;
end;
accessTracking := CkJsonObject_Create();
if (CkJsonObject_JsonTypeOf(properties,'lastAccessTimeTrackingPolicy') = 3) then
begin
success := CkJsonObject_ObjectOf2(properties,'lastAccessTimeTrackingPolicy',accessTracking);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read access tracking settings.');
Exit;
end;
CkJsonObject_Delete(accessTracking,'name');
CkJsonObject_Delete(accessTracking,'trackingGranularityInDays');
CkJsonObject_Delete(accessTracking,'blobType');
end;
CkJsonObject_UpdateBool(properties,'deleteRetentionPolicy.enabled',True);
CkJsonObject_UpdateInt(properties,'deleteRetentionPolicy.days',14);
CkJsonObject_UpdateBool(properties,'containerDeleteRetentionPolicy.enabled',True);
CkJsonObject_UpdateInt(properties,'containerDeleteRetentionPolicy.days',14);
CkJsonObject_UpdateBool(properties,'isVersioningEnabled',True);
request := CkJsonObject_Create();
success := CkJsonObject_AppendObjectCopy(request,'properties',properties);
if (success <> True) then
begin
Memo1.Lines.Add('Could not copy blob-service settings.');
Exit;
end;
// This PUT can replace service settings. Do not send only a few fields and assume
// omitted CORS rules survive. Coordinate writers around the GET and PUT.
// On a write timeout, read the resource before deciding whether to retry.
success := CkHttp_HttpJson(http,'PUT',blobUrl,request,'application/json',response);
if (CkHttpResponse_getStatusCode(response) = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect LastErrorText privately; it may contain credentials.');
Exit;
end;
if ((CkHttpResponse_getStatusCode(response) <> 200)) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkHttpResponse_getStatusCode(response)));
Memo1.Lines.Add('Request ID: ' + CkHttpResponse__getHeaderField(response,'x-ms-request-id'));
Memo1.Lines.Add('Retry-After: ' + CkHttpResponse__getHeaderField(response,'Retry-After'));
// Inspect the error body privately. Do not log tokens, headers, or full responses.
// For 401 check the token/audience; for 403 check Azure RBAC and policy.
// Honor Retry-After for 429 and use bounded backoff for transient failures.
Exit;
end;
if (success <> True) then
begin
Memo1.Lines.Add('The HTTP response could not be read completely.');
Exit;
end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
Memo1.Lines.Add('Blob protection settings updated. Retention and versions can increase storage costs.');
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkUrl_Dispose(parsed);
CkJsonObject_Dispose(properties);
CkJsonObject_Dispose(restore);
CkJsonObject_Dispose(accessTracking);
CkJsonObject_Dispose(request);