Azure-sdk-for-python: Finding the azure account key with Blob Service Client fails

Created on 12 Nov 2020  路  6Comments  路  Source: Azure/azure-sdk-for-python

I am using
Name: azure-mgmt-storage
Version: 16.0.0
Summary: Microsoft Azure Storage Management Client Library for Python
python version:3.8.0
Operating System: Windows

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>

blob_service_client.api_version evaluates to 2020-02-10.

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

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

Storage customer-reported needs-author-feedback question

All 6 comments

Hi @smoolya17

If you're looking for the Storage account keys, you can get them from the Mgmt SDK (your storage_client)

https://github.com/Azure/azure-sdk-for-python/blob/c509c6bf548ebe4918cd5f3503bcbd47ca982170/tools/azure-sdk-tools/devtools_testutils/storage_testcase.py#L68-L72

Does that answer your question?

Side note: the credentials attribute is an opaque object that tells how you are currently connected, this won't give you anything else that what you know already (since that's the information you used to already connect).

Hello Imazuel, thank you for the workaround. I will look into it.

As for the sidenote, I am trying to use the credential attribute & not credential with an 's', which I think is still under development & is not yet defined but only mentioned as a part of __dir__()

To be more clear: you shouldn't use credentials or credential at all, this is context you're supposed to have anyway already, because this is built from your input to the __init__ and add no additional value.

Touche, I agree with first part to not use credentials as it is just an object to be used for Authorization purposes.

While the name credential is an attribute defined here to be accessed & utilise it. Well I believe the name 'credential' has caused this confusion

No worries, please just let me know if the code I gave you is enough. Thanks!

Yes @lmazuel , the keys got generated & the key1.I used it with the container client to list blobs and find out the size. Hence closing this issue

Was this page helpful?
0 / 5 - 0 ratings