Azure Python SDK - 向 Blob 存储进行身份验证

Azure Python SDK - authenticate to Blob Storage

我想使用 Python SDK 操作(删除)我的 ADLS(容器)中的文件和文件夹。我有 2 个问题:

  1. 数百个 Azure SDK 中的哪个用于此目的?
  2. 如何使用 AAD 令牌进行身份验证?我真的更喜欢以这种方式进行身份验证,或者也使用服务主体的凭据(用户名、密码、租户)

我已经看过了:

并使用

进行身份验证

Which of the hundreds of Azure SDKs to use for this purpose?

考虑到 ADLS Gen2 构建在 Blob 存储之上,您可以同时使用 azure.storage.filedatalakeazure.storage.blob,但建议使用 azure.storage.filedatalake,因为此 SDK 专为 ADLS 而设计Gen2.

How to authenticate using AAD token? I really prefer authenticating this way, or also using credentials for service principal (username, password, tenant)

有关如何使用 Azure AD 连接到 Azure 存储,请参阅 Authorize access to blobs using Azure Active Directory。这里要记住的关键是,无论用户(甚至服务主体)使用 Azure AD 凭据连接到 Azure 存储,都必须为该用户分配 Azure 存储数据操作权限,例如Blob 数据贡献者。

完成后,只需创建一个凭据对象,然后使用该凭据对象连接到 Azure 存储。

例如,看看下面的代码示例,它取自 here:

from azure.identity import ClientSecretCredential
   token_credential = ClientSecretCredential(
       self.active_directory_tenant_id,
       self.active_directory_application_id,
       self.active_directory_application_secret,
   )
   datalake_service_client = DataLakeServiceClient("https://{}.dfs.core.windows.net".format(self.account_name),
                                                   credential=token_credential)