【问题标题】:Downloading Azure Stroage data using Azure AD in Python在 Python 中使用 Azure AD 下载 Azure 存储数据
【发布时间】:2021-10-31 05:56:23
【问题描述】:

我正在尝试使用 Python 在 Azure 存储容器中下载数据。使用帐户密钥不是一种选择,因此我正在尝试使用 Azure AD,但到目前为止还无法使其正常工作。我主要使用这里的文档作为参考:https://docs.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-python.

使用 Azure AD 进行连接的代码:

def initialize_storage_account_ad(storage_account_name, client_id, client_secret, tenant_id):
    
    try:  
        global service_client

        credential = ClientSecretCredential(tenant_id, client_id, client_secret)

        service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
            "https", storage_account_name), credential=credential)
    
    except Exception as e:
        print(e)

下载数据的代码:

def download_file_from_directory():
    try:
        file_system_client = service_client.get_file_system_client(file_system="my-file-system")

        directory_client = file_system_client.get_directory_client("my-directory")
        
        local_file = open("C:\\file-to-download.txt",'wb')

        file_client = directory_client.get_file_client("uploaded-file.txt")

        download = file_client.download_file()

        downloaded_bytes = download.readall()

        local_file.write(downloaded_bytes)

        local_file.close()

    except Exception as e:
     print(e)

现在我知道我已经正确设置了下载,因为我可以在使用帐户密钥时获取数据。但不知何故,使用 Azure AD 连接没有成功。我从注册应用程序开始,查找租户 ID/客户端 ID/客户端密码。我还向注册的应用授予了 Azure 存储的权限并启用了隐式授权流(ID 令牌)。我还缺少什么吗?任何帮助表示赞赏。

【问题讨论】:

  • 您面临的错误是什么?在什么时候?
  • 您还需要通过存储帐户上的 RBAC 设置获得存储 blob 数据读取器角色。针对您的服务主体设置此角色,并给它大约 5 分钟的传播时间。然后再试一次。

标签: python azure azure-active-directory azure-authentication azure-identity


【解决方案1】:

如果您使用 Azure Active Directory (Azure AD) 授权访问,请确保您分配了 Storage Blob Data Owner role 。并 Grant access 在 Azure 门户中使用 RBAC 访问 Azure Blob 数据

您必须将以下Azure role-based access control (Azure RBAC) roles 之一分配给您的安全主体。

存储 Blob 数据所有者:帐户中的所有目录和文件。

Storage Blob Data Contributor:仅限安全主体拥有的目录和文件。

更多详情请参考document

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 2021-04-19
    • 1970-01-01
    • 2021-12-26
    • 2019-08-04
    • 2020-10-05
    相关资源
    最近更新 更多