python - 无法使用 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
有任何想法吗?
解决方案
这是因为 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。
本文档提供了两种选择:
使用本文后续部分中描述的其他身份验证方法之一,该方法适用于仅使用 SDK 管理库且不会部署到云的代码,在这种情况下,您只能依赖本地服务主体。
使用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)
推荐阅读
- c# - Asp.net 中 IIS 服务器的路径错误
- c# - 是否可以在 C# 中获取属性的所有引用?
- python - 如何在 python 中正确导入包?
- javascript - Facebook 即时游戏卡在 100%
- java - 查找不包括最小值和最大值的数组的修剪平均值
- reactjs - 在表单的钩子中使用对象
- okta - 如何从我们的应用程序中调用 Okta Add Person API?
- flutter - 视频不可见,但音频在 iOS 中工作正常,颤动视频播放器
- javascript - TypeError:无法读取反应js中未定义的属性'then'
- python - 如何从pandas df中的x列中提取值,其中df == list(i)中的y列