1

Should it be possible to use cli authentication with Azure storage?

cli_auth = AzureCliAuthentication()
blob_service_client = BlobServiceClient(account_url="https://mystorage.blob.core.windows.net", credential=cli_auth)
container_client = blob_service_client.get_container_client("mycontainer")

blobs=container_client.list_blobs()

for blob in blobs:
    print(blob)

Right now I get:

Exception has occurred: ClientAuthenticationError Server failed to authenticate the request. Please refer to the information in the www-authenticate header. ErrorCode:InvalidAuthenticationInfo authenticationerrordetail:Audience validation failed. Audience did not match.

1
  • 1
    When run locally, DefaultAzureCredential relies on environment variables named AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID learn.microsoft.com/en-us/azure/developer/python/…. AzureCliCredential is one of the methods called. Your question does not show what AzureCliAuthentication does or what library it comes from. Commented Feb 15, 2022 at 6:48

1 Answer 1

1

You will have to use AzureCLICredentials instead of using AzureCLIAuthentication.

You can use something like below after doing a az login :

from azure.identity import AzureCliCredential
from azure.storage.blob import BlobServiceClient
cli_auth = AzureCliCredential()
blob_service_client = BlobServiceClient(account_url="https://<Storageaccountname>.blob.core.windows.net", credential=cli_auth)
container_client = blob_service_client.get_container_client("<ContainerName>")

blobs=container_client.list_blobs()

for blob in blobs:
    print(blob.name)

Output:

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I can reproduce the error message using AzureCliCredential as well.
Same here. I get the error This request is not authorized to perform this operation using this permission.
I was the owner of the subscription but that was not enough. I added myself to the role "Storage Blob Data Contributor" and now it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.