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.
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' 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.
set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureToken.json")
If (success <> 1) Then
outFile.WriteLine("Could not load the private Azure Resource Manager token file.")
WScript.Quit
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("A nonempty access_token string is required.")
WScript.Quit
End If
' Use your subscription ID as a string. The all-zero value is only a placeholder.
subscriptionId = "00000000-0000-0000-0000-000000000000"
set http = CreateObject("Chilkat.Http")
http.AuthToken = token.StringOf("access_token")
http.Accept = "application/json"
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 30
http.ReadTimeout = 60
success = http.SetUrlVar("subscriptionId",subscriptionId)
If (success <> 1) Then
outFile.WriteLine("Could not encode subscription ID.")
WScript.Quit
End If
set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
set parsed = CreateObject("Chilkat.Url")
resourceGroupName = "my-resource-group"
success = http.SetUrlVar("resourceGroupName",resourceGroupName)
If (success <> 1) Then
outFile.WriteLine("Could not encode resource group.")
WScript.Quit
End If
accountName = "youruniquestorageaccount"
success = http.SetUrlVar("accountName",accountName)
If (success <> 1) Then
outFile.WriteLine("Could not encode account name.")
WScript.Quit
End If
resourceUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
success = http.HttpNoBody("GET",resourceUrl,response)
If (response.StatusCode = 0) Then
outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
WScript.Quit
End If
If ((response.StatusCode <> 200)) Then
outFile.WriteLine("HTTP status: " & response.StatusCode)
outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
outFile.WriteLine("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.
WScript.Quit
End If
If (success <> 1) Then
outFile.WriteLine("The HTTP response could not be read completely.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a well-formed JSON response.")
WScript.Quit
End If
policyUrl = "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.
createMissingPolicy = 0
success = http.HttpNoBody("GET",policyUrl,response)
If ((response.StatusCode <> 200) And (response.StatusCode <> 404)) Then
outFile.WriteLine("Could not read policy. HTTP status: " & response.StatusCode)
outFile.WriteLine("Retry-After: " & response.GetHeaderField("Retry-After"))
WScript.Quit
End If
If ((response.StatusCode = 200) And (success <> 1)) Then
outFile.WriteLine("Incomplete policy response.")
WScript.Quit
End If
set request = CreateObject("Chilkat.JsonObject")
success = request.UpdateNewArray("properties.policy.rules")
set mergedRules = CreateObject("Chilkat.JsonArray")
success = request.ArrayOf2("properties.policy.rules",mergedRules)
If (success <> 1) Then
outFile.WriteLine("Could not prepare rule array.")
WScript.Quit
End If
' Only this named rule belongs to this example. Other rules are copied unchanged.
ruleName = "CoolSelectedBlobsAfter30Days"
If (response.StatusCode = 200) Then
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a well-formed JSON response.")
WScript.Quit
End If
If (result.JsonTypeOf("properties.policy.rules") <> 4) Then
outFile.WriteLine("Missing or invalid properties.policy.rules.")
WScript.Quit
End If
set existingRules = CreateObject("Chilkat.JsonArray")
success = result.ArrayOf2("properties.policy.rules",existingRules)
If (success <> 1) Then
outFile.WriteLine("Could not read existing rules.")
WScript.Quit
End If
set oldRule = CreateObject("Chilkat.JsonObject")
i = 0
Do While (i < existingRules.Size)
success = existingRules.ObjectAt2(i,oldRule)
If (success <> 1) Then
outFile.WriteLine("Invalid existing rule.")
WScript.Quit
End If
If (oldRule.JsonTypeOf("name") <> 1) Then
outFile.WriteLine("Missing or invalid name.")
WScript.Quit
End If
If (oldRule.StringOfEquals("name",ruleName,1) <> 1) Then
success = mergedRules.AddObjectCopyAt(-1,oldRule)
If (success <> 1) Then
outFile.WriteLine("Could not preserve rule.")
WScript.Quit
End If
End If
i = i + 1
Loop
Else
If (createMissingPolicy <> 1) Then
outFile.WriteLine("No policy returned (404). Verify the target, then set createMissingPolicy to true to create one.")
WScript.Quit
End If
End If
set rule = CreateObject("Chilkat.JsonObject")
success = mergedRules.AddObjectAt2(-1,rule)
If (success <> 1) Then
outFile.WriteLine("Could not add lifecycle rule.")
WScript.Quit
End If
success = rule.UpdateString("name",ruleName)
success = rule.UpdateString("type","Lifecycle")
success = rule.UpdateBool("enabled",1)
success = rule.UpdateString("definition.filters.blobTypes[0]","blockBlob")
' Prefix begins with a container name; it is not a URL or wildcard expression.
success = rule.UpdateString("definition.filters.prefixMatch[0]","my-container/archive/")
success = rule.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.
success = http.HttpJson("PUT",policyUrl,request,"application/json",response)
If (response.StatusCode = 0) Then
outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
WScript.Quit
End If
If ((response.StatusCode <> 200)) Then
outFile.WriteLine("HTTP status: " & response.StatusCode)
outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
outFile.WriteLine("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.
WScript.Quit
End If
If (success <> 1) Then
outFile.WriteLine("The HTTP response could not be read completely.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a well-formed JSON response.")
WScript.Quit
End If
outFile.WriteLine("Lifecycle policy updated. Matching blobs are processed asynchronously by Azure.")
outFile.Close