使用 BlobServiceClient 进行 Azure cli 身份验证

Azure cli authentication with BlobServiceClient

是否可以对 Azure 存储使用 cli 身份验证?

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)

现在我得到:

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.

您将不得不使用 AzureCLICredentials 而不是 AzureCLIAuthentication

您可以在执行 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)

输出: