Search code examples
python-3.xazurerestazure-storageazure-sdk-python

Finding the azure account key with Blob Service Client fails(azure python sdk)


I am using Name: azure-mgmt-storage Version: 16.0.0 Summary: Microsoft Azure Storage Management Client Library for Python Home-page: https://github.com/Azure/azure-sdk-for-python

for generating a report to find the storage container size. The snippet of my code that I am using is as below

from azure.mgmt.storage import StorageManagementClient

subscription_client = Subscription(tenant=tenant_id, client_id=client_id, secret=client_secret)
service_principals = subscription_client.credentials
subscription_id = subscription_client.find_subscription_id()
storage_client = StorageManagementClient(credential=service_principals, subscription_id=subscription_id)
storage_account_list = storage_client.storage_accounts.list()
for storage_account in storage_account_list:
    blob_service_client = BlobServiceClient(account_url=storage_account.primary_endpoints.blob,credential=service_principals)
                    account_info = blob_service_client.get_service_properties()
    keys = blob_service_client.credential.keys()

When I evaluate expression blob_service_client.credential, value is

<azure.identity._credentials.client_secret.ClientSecretCredential object at 0x05747E98>
  1. blob_service_client.api_version evaluates to 2020-02-10.

  2. And blob_service_client.credential.account_key or blob_service_client.credential.account_key() evaluates to {AttributeError}'ClientSecretCredential' object has no attribute 'account_key'

  3. or even when I try blob_service_client.credential.keys() I get {AttributeError}'ClientSecretCredential' object has no attribute 'keys' error

Any Azure expert can help me out here? Also connnection strings are another way to approach this problem where I can use:

BlobServiceClient.from_connection_string(connection_string)

for which I am also required to generate the connection_string dynamically, which I am unable to.


Solution

  • Since you are already using the client secret credential, you can do your storage operation (calculating storage container size in this case). Note below in my code, I had the subscription id handy already, so I did not use subscription client. But you can definitely like your original code.

    from azure.identity import ClientSecretCredential
    from azure.mgmt.storage import StorageManagementClient
    from azure.storage.blob import BlobServiceClient, ContainerClient
    
    tenant_id='<tenant id>'
    client_id='<client id>'
    client_secret='<secret>'
    subscription_id='<subscription id>'
    
    credentials = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
    storage_client = StorageManagementClient(credential=credentials, subscription_id=subscription_id)
    storage_account_list = storage_client.storage_accounts.list()
    for storage_account in storage_account_list:
        blob_service_client = BlobServiceClient(account_url=storage_account.primary_endpoints.blob,credential=credentials)
        containers = blob_service_client.list_containers()
        for container in containers:
            container_client = ContainerClient(account_url=storage_account.primary_endpoints.blob,credential=credentials, container_name=container.name)
            blobs = container_client.list_blobs()
            container_size = 0
            for blob in blobs:
                container_size = container_size + blob.size
            print('Storage Account: ' + storage_account.name + ' ; Container: ' + container.name + ' ; Size: ' + str(container_size))