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 Objective-C Downloads
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoUrl.h>
BOOL success = NO;
// 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.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
token.Strict = YES;
success = [token LoadFile: @"qa_data/tokens/azureToken.json"];
if (success != YES) {
NSLog(@"%@",@"Could not load the private Azure Resource Manager token file.");
return;
}
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"A nonempty access_token string is required.");
return;
}
// Use your subscription ID as a string. The all-zero value is only a placeholder.
NSString *subscriptionId = @"00000000-0000-0000-0000-000000000000";
CkoHttp *http = [[CkoHttp alloc] init];
http.AuthToken = [token StringOf: @"access_token"];
http.Accept = @"application/json";
http.RequireSslCertVerify = YES;
http.FollowRedirects = NO;
http.ConnectTimeout = [NSNumber numberWithInt:30];
http.ReadTimeout = [NSNumber numberWithInt:60];
success = [http SetUrlVar: @"subscriptionId" value: subscriptionId];
if (success != YES) {
NSLog(@"%@",@"Could not encode subscription ID.");
return;
}
CkoHttpResponse *response = [[CkoHttpResponse alloc] init];
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.Strict = YES;
CkoUrl *parsed = [[CkoUrl alloc] init];
NSString *resourceGroupName = @"my-resource-group";
success = [http SetUrlVar: @"resourceGroupName" value: resourceGroupName];
if (success != YES) {
NSLog(@"%@",@"Could not encode resource group.");
return;
}
NSString *accountName = @"youruniquestorageaccount";
success = [http SetUrlVar: @"accountName" value: accountName];
if (success != YES) {
NSLog(@"%@",@"Could not encode account name.");
return;
}
NSString *resourceUrl = @"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01";
success = [http HttpNoBody: @"GET" url: resourceUrl response: response];
if ([response.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Inspect LastErrorText privately; it may contain credentials.");
return;
}
if (([response.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"HTTP status: ",[response.StatusCode intValue]);
NSLog(@"%@%@",@"Request ID: ",[response GetHeaderField: @"x-ms-request-id"]);
NSLog(@"%@%@",@"Retry-After: ",[response GetHeaderField: @"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.
return;
}
if (success != YES) {
NSLog(@"%@",@"The HTTP response could not be read completely.");
return;
}
success = [result Load: response.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a well-formed JSON response.");
return;
}
// This demonstration targets standard StorageV2 without hierarchical namespace.
if (([result StringOfEquals: @"kind" value: @"StorageV2" caseSensitive: YES] != YES) || ([result StringOfEquals: @"sku.tier" value: @"Standard" caseSensitive: YES] != YES) || ([result BoolOf: @"properties.isHnsEnabled"] == YES)) {
NSLog(@"%@",@"This versioning example requires standard StorageV2 without hierarchical namespace.");
return;
}
NSString *blobUrl = @"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/blobServices/default?api-version=2026-09-01";
success = [http HttpNoBody: @"GET" url: blobUrl response: response];
if ([response.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Inspect LastErrorText privately; it may contain credentials.");
return;
}
if (([response.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"HTTP status: ",[response.StatusCode intValue]);
NSLog(@"%@%@",@"Request ID: ",[response GetHeaderField: @"x-ms-request-id"]);
NSLog(@"%@%@",@"Retry-After: ",[response GetHeaderField: @"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.
return;
}
if (success != YES) {
NSLog(@"%@",@"The HTTP response could not be read completely.");
return;
}
success = [result Load: response.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a well-formed JSON response.");
return;
}
if ([[result JsonTypeOf: @"properties"] intValue] != 3) {
NSLog(@"%@",@"Missing or invalid properties.");
return;
}
CkoJsonObject *properties = [[CkoJsonObject alloc] init];
success = [result ObjectOf2: @"properties" jsonObj: properties];
if (success != YES) {
NSLog(@"%@",@"Could not read blob-service properties.");
return;
}
// Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
[properties Delete: @"automaticSnapshotPolicyEnabled"];
CkoJsonObject *restore = [[CkoJsonObject alloc] init];
if ([[properties JsonTypeOf: @"restorePolicy"] intValue] == 3) {
success = [properties ObjectOf2: @"restorePolicy" jsonObj: restore];
if (success != YES) {
NSLog(@"%@",@"Could not read restore policy.");
return;
}
[restore Delete: @"lastEnabledTime"];
[restore Delete: @"minRestoreTime"];
if (([restore BoolOf: @"enabled"] == YES) && ([[restore IntOf: @"days"] intValue] >= 14)) {
NSLog(@"%@",@"Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately.");
return;
}
}
CkoJsonObject *accessTracking = [[CkoJsonObject alloc] init];
if ([[properties JsonTypeOf: @"lastAccessTimeTrackingPolicy"] intValue] == 3) {
success = [properties ObjectOf2: @"lastAccessTimeTrackingPolicy" jsonObj: accessTracking];
if (success != YES) {
NSLog(@"%@",@"Could not read access tracking settings.");
return;
}
[accessTracking Delete: @"name"];
[accessTracking Delete: @"trackingGranularityInDays"];
[accessTracking Delete: @"blobType"];
}
[properties UpdateBool: @"deleteRetentionPolicy.enabled" value: YES];
[properties UpdateInt: @"deleteRetentionPolicy.days" value: [NSNumber numberWithInt: 14]];
[properties UpdateBool: @"containerDeleteRetentionPolicy.enabled" value: YES];
[properties UpdateInt: @"containerDeleteRetentionPolicy.days" value: [NSNumber numberWithInt: 14]];
[properties UpdateBool: @"isVersioningEnabled" value: YES];
CkoJsonObject *request = [[CkoJsonObject alloc] init];
success = [request AppendObjectCopy: @"properties" jsonObj: properties];
if (success != YES) {
NSLog(@"%@",@"Could not copy blob-service settings.");
return;
}
// 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 = [http HttpJson: @"PUT" url: blobUrl json: request contentType: @"application/json" response: response];
if ([response.StatusCode intValue] == 0) {
NSLog(@"%@",@"No HTTP response. Inspect LastErrorText privately; it may contain credentials.");
return;
}
if (([response.StatusCode intValue] != 200)) {
NSLog(@"%@%d",@"HTTP status: ",[response.StatusCode intValue]);
NSLog(@"%@%@",@"Request ID: ",[response GetHeaderField: @"x-ms-request-id"]);
NSLog(@"%@%@",@"Retry-After: ",[response GetHeaderField: @"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.
return;
}
if (success != YES) {
NSLog(@"%@",@"The HTTP response could not be read completely.");
return;
}
success = [result Load: response.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a well-formed JSON response.");
return;
}
NSLog(@"%@",@"Blob protection settings updated. Retention and versions can increase storage costs.");