Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.6.0+

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.

Microsoft API reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkUrlW.h>

void ChilkatSample(void)
    {
    bool 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.
    CkJsonObjectW token;
    token.put_Strict(true);
    success = token.LoadFile(L"qa_data/tokens/azureToken.json");
    if (success != true) {
        wprintf(L"Could not load the private Azure Resource Manager token file.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"A nonempty access_token string is required.\n");
        return;
    }

    //  Use your subscription ID as a string. The all-zero value is only a placeholder.
    const wchar_t *subscriptionId = L"00000000-0000-0000-0000-000000000000";
    CkHttpW http;
    http.put_AuthToken(token.stringOf(L"access_token"));
    http.put_Accept(L"application/json");
    http.put_RequireSslCertVerify(true);
    http.put_FollowRedirects(false);
    http.put_ConnectTimeout(30);
    http.put_ReadTimeout(60);
    success = http.SetUrlVar(L"subscriptionId",subscriptionId);
    if (success != true) {
        wprintf(L"Could not encode subscription ID.\n");
        return;
    }

    CkHttpResponseW response;
    CkJsonObjectW result;
    result.put_Strict(true);
    CkUrlW parsed;
    const wchar_t *resourceGroupName = L"my-resource-group";
    success = http.SetUrlVar(L"resourceGroupName",resourceGroupName);
    if (success != true) {
        wprintf(L"Could not encode resource group.\n");
        return;
    }

    const wchar_t *accountName = L"youruniquestorageaccount";
    success = http.SetUrlVar(L"accountName",accountName);
    if (success != true) {
        wprintf(L"Could not encode account name.\n");
        return;
    }

    const wchar_t *resourceUrl = L"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01";
    success = http.HttpNoBody(L"GET",resourceUrl,response);
    if (response.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Inspect LastErrorText privately; it may contain credentials.\n");
        return;
    }

    if ((response.get_StatusCode() != 200)) {
        wprintf(L"HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
        wprintf(L"Retry-After: %s\n",response.getHeaderField(L"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 != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    //  This demonstration targets standard StorageV2 without hierarchical namespace.
    if ((result.StringOfEquals(L"kind",L"StorageV2",true) != true) || (result.StringOfEquals(L"sku.tier",L"Standard",true) != true) || (result.BoolOf(L"properties.isHnsEnabled") == true)) {
        wprintf(L"This versioning example requires standard StorageV2 without hierarchical namespace.\n");
        return;
    }

    const wchar_t *blobUrl = L"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/blobServices/default?api-version=2026-09-01";
    success = http.HttpNoBody(L"GET",blobUrl,response);
    if (response.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Inspect LastErrorText privately; it may contain credentials.\n");
        return;
    }

    if ((response.get_StatusCode() != 200)) {
        wprintf(L"HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
        wprintf(L"Retry-After: %s\n",response.getHeaderField(L"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 != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    if (result.JsonTypeOf(L"properties") != 3) {
        wprintf(L"Missing or invalid properties.\n");
        return;
    }

    CkJsonObjectW properties;
    success = result.ObjectOf2(L"properties",properties);
    if (success != true) {
        wprintf(L"Could not read blob-service properties.\n");
        return;
    }

    //  Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
    properties.Delete(L"automaticSnapshotPolicyEnabled");
    CkJsonObjectW restore;
    if (properties.JsonTypeOf(L"restorePolicy") == 3) {
        success = properties.ObjectOf2(L"restorePolicy",restore);
        if (success != true) {
            wprintf(L"Could not read restore policy.\n");
            return;
        }

        restore.Delete(L"lastEnabledTime");
        restore.Delete(L"minRestoreTime");
        if ((restore.BoolOf(L"enabled") == true) && (restore.IntOf(L"days") >= 14)) {
            wprintf(L"Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately.\n");
            return;
        }

    }

    CkJsonObjectW accessTracking;
    if (properties.JsonTypeOf(L"lastAccessTimeTrackingPolicy") == 3) {
        success = properties.ObjectOf2(L"lastAccessTimeTrackingPolicy",accessTracking);
        if (success != true) {
            wprintf(L"Could not read access tracking settings.\n");
            return;
        }

        accessTracking.Delete(L"name");
        accessTracking.Delete(L"trackingGranularityInDays");
        accessTracking.Delete(L"blobType");
    }

    properties.UpdateBool(L"deleteRetentionPolicy.enabled",true);
    properties.UpdateInt(L"deleteRetentionPolicy.days",14);
    properties.UpdateBool(L"containerDeleteRetentionPolicy.enabled",true);
    properties.UpdateInt(L"containerDeleteRetentionPolicy.days",14);
    properties.UpdateBool(L"isVersioningEnabled",true);
    CkJsonObjectW request;
    success = request.AppendObjectCopy(L"properties",properties);
    if (success != true) {
        wprintf(L"Could not copy blob-service settings.\n");
        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(L"PUT",blobUrl,request,L"application/json",response);
    if (response.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Inspect LastErrorText privately; it may contain credentials.\n");
        return;
    }

    if ((response.get_StatusCode() != 200)) {
        wprintf(L"HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
        wprintf(L"Retry-After: %s\n",response.getHeaderField(L"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 != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    wprintf(L"Blob protection settings updated. Retention and versions can increase storage costs.\n");
    }