azure 使用Python Sdk识别没有任何资源(如表、容器等)的存储帐户

hlswsv35  于 2023-03-09  发布在  Python
关注(0)|答案(1)|浏览(128)

列出在Azure自动化帐户python版本3.8上使用python sdk时没有容器、表、队列和文件共享的存储帐户。下面是我尝试获取的脚本,但遇到错误**,因为containers = list(storage_client.blob_containers.list(resource_group_name,account_name))类型错误:“ListContainerItems”对象不可迭代**

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.storage import StorageManagementClient
client_id = 'XXX'
tenant_id = 'XXX'
client_secret = 'XXX'
subscription_id = ('subscription_id')
credentials = ServicePrincipalCredentials(client_id, client_secret, tenant=tenant_id)
from azure.mgmt.storage import StorageManagementClient
storage_client = StorageManagementClient(credentials, subscription_id)

# Get a list of all storage accounts in the subscription
storage_accounts = storage_client.storage_accounts.list()

# Loop through each storage account and check if it has containers, tables, queues, or file shares
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # Check if the storage account has containers
    containers = list(storage_client.blob_containers.list(resource_group_name, account_name))
    #print(containers)
    if containers:
        continue

    # Check if the storage account has tables
    tables = list(storage_client.table.list(resource_group_name, account_name))
    if tables:
        continue

    # Check if the storage account has queues
    queues = list(storage_client.queue.list(resource_group_name, account_name))
    if queues:
        continue

    # Check if the storage account has file shares
    file_shares = list(storage_client.file_shares.list(resource_group_name, account_name))
    if file_shares:
        continue

    # If none of the above resources are found, print the storage account name as orphan
    print(f"Orphaned Storage account name {account_name}.")
ghg1uchk

ghg1uchk1#

从我的终端复制后,我可以使用下面的代码完成此操作。

#!/usr/bin/env python3

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.storage import StorageManagementClient
client_id = '<CLIENT_ID>'
tenant_id = '<TENANT_ID>'
client_secret = '<CLIENT_SECRET>'
subscription_id = ('<SUBSCRIPTION_ID>')

credentials = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

storage_client = StorageManagementClient(credentials, subscription_id)

# Get a list of all storage accounts in the subscription
storage_accounts = storage_client.storage_accounts.list()

# Loop through each storage account and check if it has containers, tables, queues, or file shares
for account in storage_accounts:
    account_name = account.name
    resource_group_name = account.id.split("/")[4]
    
    # Check if the storage account has containers
    containers = list(storage_client.blob_containers.list(resource_group_name, account_name))
    tables = list(storage_client.table.list(resource_group_name, account_name))
    queues = list(storage_client.queue.list(resource_group_name, account_name))
    file_shares = list(storage_client.file_shares.list(resource_group_name, account_name))
    
    if(len(containers) and len(list(tables)) and len(queues) and len(file_shares) == 0):
        print(f"Orphaned Storage account name {account_name}.")
        
    else:
        print("Not a Orphaned Storage")

下面是我安装的python模块。

结果:

相关问题