azure-blob-storageazure-cliazure-python-sdkazureclicredential

Azure cli authentication with BlobServiceClient


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.


Solution

  • 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