python - 如何使用Python获取Azure的所有存储帐户名称及其访问 key

标签 python azure cloud azure-storage

我无法读取所有 Azure 存储帐户名称及其 key 。

AZURE_TENANT_ID = '<string>'
AZURE_CLIENT_ID = '<string>'
AZURE_CLIENT_SECRET = '<string>'
AZURE_SUBSCRIPTION_ID = '<string>'
import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.storage.models import (
     StorageAccountCreateParameters,
     StorageAccountUpdateParameters,
     Sku,
     SkuName,
     Kind
    )

subscription_id = AZURE_SUBSCRIPTION_ID # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client_id=AZURE_CLIENT_ID,
secret=AZURE_CLIENT_SECRET,
tenant=AZURE_TENANT_ID
)
resource_client = ResourceManagementClient(credentials, subscription_id)
storage_client = StorageManagementClient(credentials, subscription_id)
print(resource_client,storage_client)
# Retrieve the list of resource groups
for item in storage_client.storage_accounts.list():
    print_item(item)

在这段代码之后我得到了这个错误

AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'

在调试时我发现“storage_client.storage_accounts.list()”这个语句返回azure.core.paging.ItemPaged类的迭代器对象,并且它始终返回相同的对象

请帮帮我

最佳答案

我们创建了以下 python 脚本来提取特定订阅下的存储帐户及其各自的访问 key 的列表。

在下面的代码中,我们使用了 azure.identity 下的 ClientSecretCredential 库,而不是使用 token 进行身份验证的较新库 ServicePrincipalCredentials基于 Azure documentation 的凭据.

这是Python代码:

AZURE_TENANT_ID  =  '<tenantid>'
AZURE_CLIENT_ID  =  '<clientid>'
AZURE_CLIENT_SECRET  =  '<clientsecret>'
AZURE_SUBSCRIPTION_ID  =  '<subscriptionid>'
    
import  os
from  azure.identity  import  ClientSecretCredential
from  azure.mgmt.resource  import  ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.storage.models import  (StorageAccountCreateParameters,StorageAccountUpdateParameters,
Sku,SkuName,Kind)
    
subscription_id  =  AZURE_SUBSCRIPTION_ID  # your Azure Subscription Id
credentials  =  ClientSecretCredential(tenant_id=AZURE_TENANT_ID,client_id=AZURE_CLIENT_ID,client_secret=AZURE_CLIENT_SECRET)
resource_client  =  ResourceManagementClient(credentials,  subscription_id)
storage_client  = StorageManagementClient(credentials,  subscription_id)
    
# Retrieve the list of resource groups
resourcelist=resource_client.resource_groups.list()
for  item  in  resourcelist:
    for  item1  in  resource_client.resources.list_by_resource_group(item.name):
        if(item1.type=='Microsoft.Storage/storageAccounts'):
            storage_keys  =  storage_client.storage_accounts.list_keys(item.name,  item1.name)
            storage_keys  =  {v.key_name:  v.value for  v  in  storage_keys.keys}
            print(item.name,('\tKey 1: {}'.format(storage_keys['key1'])))
            print(item.name,('\tKey 2: {}'.format(storage_keys['key2'])))

这是供引用的输出屏幕截图:

enter image description here

注意:

您在上述代码中用于身份验证的服务主体需要具有整个订阅的 RBAC 角色storage Account Contributor

关于python - 如何使用Python获取Azure的所有存储帐户名称及其访问 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69819858/

相关文章:

python - PhantomJS 在 Selenium : WebDriverException with status code 127 上意外退出

python - Databricks - 启动 REPL 失败

azure - Docker for Windows - 文件共享

c# - 使用 C# 代码或 .net 以编程方式在 windows azure 中创建虚拟机

.net - 如何在Azure中发现角色实例?

python - Python 的 tarfile.open 需要 close() 吗?

python - 神经网络在 1000 个时期后不学习来解决 XOR 问题

azure - 列出所有 keyvault secret

sql - Chef Cookbook 无法使用执行资源运行 sqllocaldb

python - 在python中使用azure sdk从VM对象获取IP