首页 > 解决方案 > 无法使用 DefaultAzureCredential 创建 DataFactoryManagementClient()?

问题描述

到目前为止,我DefaultAzureCredential用于所有 Python 开发人员(KeyVault 客户端、BlobStorage 客户端等)。这是我第一次在 Python 中使用 azure.mgmt.datafactory。尝试使用DefaultAzureCredential时,我收到错误:AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session'.

重现: 重现行为的步骤:

credential = DefaultAzureCredential()
resource_client = ResourceManagementClient(credential, subscription_id)
adf_client = DataFactoryManagementClient(credential, subscription_id)
rg_name = 'RG_ADF_SANDBOX1'
df_name = 'df20201019test'
df_resource = Factory(location='westus')
df = adf_client.factories.create_or_update(rg_name, df_name, df_resource) #<----ERROR HERE

有任何想法吗?

标签: pythonauthenticationazure-data-factory

解决方案


这是因为 azure-mgmt-datafactory 尚未更新为使用 azure.core。您可以参考https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate?tabs=cmd#defaultazurecredential-object-has-no-attribute-signed-session

本文档提供了两种选择:

  1. 使用本文后续部分中描述的其他身份验证方法之一,该方法适用于仅使用 SDK 管理库且不会部署到云的代码,在这种情况下,您只能依赖本地服务主体。

  2. 使用Azure SDK 工程团队成员提供的 CredentialWrapper 类 ( cred_wrapper.py ) 而不是 DefaultAzureCredential。所需的管理库可用后,切换回 DefaultAzureCredential。此方法的优点是您可以对 SDK 客户端和管理库使用相同的凭据,并且在本地和云中都可以使用。

您还可以使用以下代码创建数据工厂:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.datafactory import DataFactoryManagementClient
from azure.mgmt.datafactory.models import *
import time

#Create a data factory
subscription_id = '<Specify your Azure Subscription ID>'
credentials = ServicePrincipalCredentials(client_id='<Active Directory application/client ID>', secret='<client secret>', tenant='<Active Directory tenant ID>')
adf_client = DataFactoryManagementClient(credentials, subscription_id)

rg_params = {'location':'eastus'}
df_params = {'location':'eastus'}  

df_resource = Factory(location='eastus')
df = adf_client.factories.create_or_update(rg_name, df_name, df_resource)
print_item(df)
while df.provisioning_state != 'Succeeded':
    df = adf_client.factories.get(rg_name, df_name)
    time.sleep(1)

推荐阅读