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

Azure Storage Accounts: Add or Update a Lifecycle Tiering Rule

See more Azure Storage Accounts Examples

Reads the lifecycle policy, replaces only the named rule, and preserves other rules. The new rule moves matching block blobs to Cool after 30 days without modification, scoped by container/blob prefix. It adds no deletion action; existing rules retain their actions.

Review the prefix, existing rule ownership, account support, and tiering/retrieval charges. A policy update schedules background processing; it does not immediately move blobs. The policy PUT replaces the document, so coordinate concurrent writers. If the policy GET returns 404, the default is to stop. Verify that no policy exists, then set createMissingPolicy=true to start a new one.

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 AutoIt Downloads

AutoIt
Local $bSuccess = 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.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/azureToken.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private Azure Resource Manager token file." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("A nonempty access_token string is required." & @CRLF)
    Exit
EndIf

;  Use your subscription ID as a string. The all-zero value is only a placeholder.
Local $subscriptionId = "00000000-0000-0000-0000-000000000000"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.Accept = "application/json"
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 30
$oHttp.ReadTimeout = 60
$bSuccess = $oHttp.SetUrlVar("subscriptionId",$subscriptionId)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode subscription ID." & @CRLF)
    Exit
EndIf

$oResponse = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$oParsed = ObjCreate("Chilkat.Url")
Local $sResourceGroupName = "my-resource-group"
$bSuccess = $oHttp.SetUrlVar("resourceGroupName",$sResourceGroupName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode resource group." & @CRLF)
    Exit
EndIf

Local $sAccountName = "youruniquestorageaccount"
$bSuccess = $oHttp.SetUrlVar("accountName",$sAccountName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode account name." & @CRLF)
    Exit
EndIf

Local $sResourceUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
$bSuccess = $oHttp.HttpNoBody("GET",$sResourceUrl,$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  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
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

Local $sPolicyUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/managementPolicies/default?api-version=2026-09-01"

;  Read the account first so a nonexistent account is not mistaken for no policy.
;  Opt in after confirming that a 404 here means no policy has been configured.
Local $bCreateMissingPolicy = False
$bSuccess = $oHttp.HttpNoBody("GET",$sPolicyUrl,$oResponse)
If (($oResponse.StatusCode <> 200) And ($oResponse.StatusCode <> 404)) Then
    ConsoleWrite("Could not read policy. HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode = 200) And ($bSuccess <> True)) Then
    ConsoleWrite("Incomplete policy response." & @CRLF)
    Exit
EndIf

$oRequest = ObjCreate("Chilkat.JsonObject")
$oRequest.UpdateNewArray("properties.policy.rules")
$oMergedRules = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oRequest.ArrayOf2("properties.policy.rules",$oMergedRules)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not prepare rule array." & @CRLF)
    Exit
EndIf

;  Only this named rule belongs to this example. Other rules are copied unchanged.
Local $sRuleName = "CoolSelectedBlobsAfter30Days"
If ($oResponse.StatusCode = 200) Then
    $bSuccess = $oResult.Load($oResponse.BodyStr)
    If ($bSuccess <> True) Then
        ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
        Exit
    EndIf

    If ($oResult.JsonTypeOf("properties.policy.rules") <> 4) Then
        ConsoleWrite("Missing or invalid properties.policy.rules." & @CRLF)
        Exit
    EndIf

    $oExistingRules = ObjCreate("Chilkat.JsonArray")
    $bSuccess = $oResult.ArrayOf2("properties.policy.rules",$oExistingRules)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not read existing rules." & @CRLF)
        Exit
    EndIf

    $oOldRule = ObjCreate("Chilkat.JsonObject")
Local $i = 0
    While ($i < $oExistingRules.Size)
        $bSuccess = $oExistingRules.ObjectAt2($i,$oOldRule)
        If ($bSuccess <> True) Then
            ConsoleWrite("Invalid existing rule." & @CRLF)
            Exit
        EndIf

        If ($oOldRule.JsonTypeOf("name") <> 1) Then
            ConsoleWrite("Missing or invalid name." & @CRLF)
            Exit
        EndIf

        If ($oOldRule.StringOfEquals("name",$sRuleName,True) <> True) Then
            $bSuccess = $oMergedRules.AddObjectCopyAt(-1,$oOldRule)
            If ($bSuccess <> True) Then
                ConsoleWrite("Could not preserve rule." & @CRLF)
                Exit
            EndIf

        EndIf

        $i = $i + 1
    Wend
Else
    If ($bCreateMissingPolicy <> True) Then
        ConsoleWrite("No policy returned (404). Verify the target, then set createMissingPolicy to true to create one." & @CRLF)
        Exit
    EndIf

EndIf

$oRule = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oMergedRules.AddObjectAt2(-1,$oRule)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not add lifecycle rule." & @CRLF)
    Exit
EndIf

$oRule.UpdateString("name",$sRuleName)
$oRule.UpdateString("type","Lifecycle")
$oRule.UpdateBool("enabled",True)
$oRule.UpdateString("definition.filters.blobTypes[0]","blockBlob")
;  Prefix begins with a container name; it is not a URL or wildcard expression.
$oRule.UpdateString("definition.filters.prefixMatch[0]","my-container/archive/")
$oRule.UpdateInt("definition.actions.baseBlob.tierToCool.daysAfterModificationGreaterThan",30)
;  No delete action is added. Existing rules, including their actions, remain.
;  PUT replaces the policy document; coordinate writers to avoid losing updates.
;  On a write timeout, read the resource before deciding whether to retry.
$bSuccess = $oHttp.HttpJson("PUT",$sPolicyUrl,$oRequest,"application/json",$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  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
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

ConsoleWrite("Lifecycle policy updated. Matching blobs are processed asynchronously by Azure." & @CRLF)