简体   繁体   English

Azure 使用 BlobServiceClient 进行 cli 身份验证

[英]Azure cli authentication with BlobServiceClient

Should it be possible to use cli authentication with Azure storage?是否可以对 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)

Right now I get:现在我得到:

Exception has occurred: ClientAuthenticationError Server failed to authenticate the request.发生异常:ClientAuthenticationError 服务器无法验证请求。 Please refer to the information in the www-authenticate header. ErrorCode:InvalidAuthenticationInfo authenticationerrordetail:Audience validation failed.请参考www-authenticate header中的信息。 ErrorCode:InvalidAuthenticationInfo authenticationerrordetail:Audience validation failed。 Audience did not match.观众不匹配。

You will have to use AzureCLICredentials instead of using AzureCLIAuthentication .您将必须使用AzureCLICredentials而不是使用AzureCLIAuthentication

You can use something like below after doing a az login :在执行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: Output:

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM