无法以编程方式获取 Azure 存储帐户的密钥

Unable to programmatically get the keys for Azure Storage Account

我正在尝试获取订阅中所有存储帐户的密钥。但是我在使用 Azure Java SDK 的存储帐户之一上遇到了这个异常。

com.microsoft.azure.CloudException: Status code 403, {"error":{"code":"AuthorizationFailed","message":"The client '5c79ee27-#########-######91f8e3' with object id '5c79ee27-###########-#####91f8e3' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over scope '/subscriptions/758ad253-#######f07/resourceGroups/spcwestus/providers/Microsoft.Storage/storageAccounts/grsstorag'."}}: The client '5c79ee27-###########-#####91f8e3' with object id '5c79ee27-###########-#####91f8e3' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over scope '/subscriptions/758ad253-#######f07/resourceGroups/spcwestus/providers/Microsoft.Storage/storageAccounts/grsstorag'.

对于大多数存储帐户,我设法获得了密钥。但是对于他们中的一些人来说,同样的错误会重复出现。这是什么意思?

根据您遇到的错误,服务器以 HTTP 403 状态代码响应您的请求表明您的请求已被 Forbidden服务器。

HTTP 403 Status Code

A server that receives valid credentials that are not adequate to gain access ought to respond with the 403 (Forbidden) status code
(Section 6.5.3 of [RFC7231]).

这很可能是由某些基于角色的访问控制引起的,这些访问控制旨在防止您的帐户基于某些存储帐户执行列表存储帐户密钥操作您分配的角色。

您可以使用下面的 Azure PowerShell cmdlet 来验证在特定范围内分配给您的特定对象的角色是否具有列出存储帐户密钥的权限。

Get-AzureRmRoleAssignment -Scope "/subscriptions/758ad253-#######f07/resourceGroups/spcwestus/providers/Microsoft.Storage/storageAccounts/grsstorag" -ObjectId 5c79ee27-###########-#####91f8e3

更多信息请参考下面的link:

Use Role-Based Access Control to manage access to your Azure subscription resources

也许作为上面的补充。 Azure 具有 Storage Account Key Operator Service 角色,描述如下:

  • 允许存储帐户密钥操作员列出和重新生成存储帐户上的密钥

授予您此角色应该允许列出密钥。