Azure Storage Accounts: Retrieve and Optionally Rotate a Storage Account Key
See more Azure Storage Accounts Examples
Uses List Keys to retrieve an account key, or regenerates only key1 when rotateKey=true. Saves only the selected secret to a private file and never prints it. The caller needs List Keys permission and, for rotation, Regenerate Key permission.
Regeneration immediately invalidates the old key and SAS signatures derived from it. First migrate all dependent clients to the other key; regenerate the unused key, distribute its new value securely, and only later consider rotating the other key. Do not regenerate both together. A failed local save does not undo rotation; recover using List Keys. Prefer Entra authentication where supported. See account key management.
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 AutoIt Downloads
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 $sKeysUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/listKeys?api-version=2026-09-01"
$bSuccess = $oHttp.HttpNoBody("POST",$sKeysUrl,$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
If ($oResult.JsonTypeOf("keys") <> 4) Then
ConsoleWrite("Missing or invalid keys." & @CRLF)
Exit
EndIf
; Move all clients and key-signed SAS dependencies away from key1 BEFORE rotating it.
; This example retrieves key1, then optionally regenerates key1 only.
Local $sKeyName = "key1"
Local $bRotateKey = False
If ($bRotateKey = True) Then
$oRequest = ObjCreate("Chilkat.JsonObject")
$oRequest.UpdateString("keyName",$sKeyName)
$bSuccess = $oHttp.HttpJson("POST","https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/regenerateKey?api-version=2026-09-01",$oRequest,"application/json",$oResponse)
; Do not retry regeneration after an uncertain transport result: list keys
; to recover the current value. Repeating regeneration invalidates it again.
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
If ($oResult.JsonTypeOf("keys") <> 4) Then
ConsoleWrite("Missing or invalid keys." & @CRLF)
Exit
EndIf
EndIf
$oKeys = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oResult.ArrayOf2("keys",$oKeys)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read returned keys." & @CRLF)
Exit
EndIf
$oKey = ObjCreate("Chilkat.JsonObject")
$oPrivateKey = ObjCreate("Chilkat.JsonObject")
Local $bFound = False
Local $i = 0
While ($i < $oKeys.Size)
$bSuccess = $oKeys.ObjectAt2($i,$oKey)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid key entry." & @CRLF)
Exit
EndIf
If ($oKey.StringOfEquals("keyName",$sKeyName,True) = True) Then
If (($oKey.JsonTypeOf("value") <> 1) Or ($oKey.StringOfEquals("value","",True) = True)) Then
ConsoleWrite("Selected key has no value. Inspect permissions privately." & @CRLF)
Exit
EndIf
$oPrivateKey.UpdateString("account_name",$sAccountName)
$oPrivateKey.UpdateString("key_name",$sKeyName)
$oPrivateKey.UpdateString("access_key",$oKey.StringOf("value"))
$bFound = True
EndIf
$i = $i + 1
Wend
If ($bFound <> True) Then
ConsoleWrite("Selected key was not returned." & @CRLF)
Exit
EndIf
; Pre-create this private directory with restrictive OS permissions. This file
; contains an account-wide secret; never commit it or put it in web content.
$bSuccess = $oPrivateKey.WriteFile("qa_data/azure/storage-key.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save key privately. If rotated, use List Keys to recover; do not regenerate again." & @CRLF)
Exit
EndIf
ConsoleWrite("Selected key saved privately. Key name: " & $sKeyName & @CRLF)
ConsoleWrite("Key regeneration requested: " & $bRotateKey & @CRLF)
; Distribute the current key through your application's secret-management process.
; Rotate the other key in a separate maintenance step after clients are migrated.