gpt4 book ai didi

python - Azure数据工厂管道: Creating pipelines with Python: Authentication (via az cli)

转载 作者:行者123 更新时间:2023-12-01 01:04:10 25 4
gpt4 key购买 nike

我正在尝试使用 Microsoft 提供的示例通过 python 创建 azure 数据工厂管道:

https://learn.microsoft.com/en-us/azure/data-factory/quickstart-create-data-factory-python

def main():

# Azure subscription ID
subscription_id = '<Specify your Azure Subscription ID>'

# This program creates this resource group. If it's an existing resource group, comment out the code that creates the resource group
rg_name = 'ADFTutorialResourceGroup'

# The data factory name. It must be globally unique.
df_name = '<Specify a name for the data factory. It must be globally unique>'

# Specify your Active Directory client ID, client secret, and tenant ID
credentials = ServicePrincipalCredentials(client_id='<Active Directory application/client ID>', secret='<client secret>', tenant='<Active Directory tenant ID>')
resource_client = ResourceManagementClient(credentials, subscription_id)
adf_client = DataFactoryManagementClient(credentials, subscription_id)

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

但是,我无法传递如上所示的凭据,因为 azure 登录是作为管道中较早的单独步骤执行的,从而为我留下了经过身份验证的 azure session (不能将其他凭据传递到此脚本中)。

在运行 python 代码来创建管道之前,我通过 Jenkins 部署管道进行“az 登录”,这会为我提供经过身份验证的 azurerm session 。我应该能够在 python 脚本中重复使用此 session 来获取数据工厂客户端,而无需再次进行身份验证。

但是,我不确定如何修改代码的客户端创建部分,因为似乎没有任何使用已建立的 azurerm session 的示例:

    adf_client = DataFactoryManagementClient(credentials, subscription_id)

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

#Create a data factory
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)

Microsoft 的身份验证文档建议我可以使用之前建立的 session 进行身份验证,如下所示:

from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient

client = get_client_from_cli_profile(ComputeManagementClient)

(引用:https://learn.microsoft.com/en-us/python/azure/python-sdk-azure-authenticate?view=azure-python)

这可行,但是 azure 数据工厂对象实例化失败:

Traceback (most recent call last):
File "post-scripts/check-data-factory.py", line 72, in <module>
main()
File "post-scripts/check-data-factory.py", line 65, in main
df = adf_client.factories.create_or_update(rg_name, data_factory_name, df_resource)

AttributeError: 'ComputeManagementClient' object has no attribute 'factories'

那么在这和获取 df 对象之间可能需要一些额外的步骤?

任何线索表示赞赏!

最佳答案

只需将类替换为正确的类型即可:

from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.datafactory import DataFactoryManagementClient

resource_client = get_client_from_cli_profile(ResourceManagementClient)
adf_client = get_client_from_cli_profile(DataFactoryManagementClient)

您收到的错误是因为您创建了一个计算客户端(用于处理 VM),而不是 ADF 客户端。但是,是的,您找到了适合您需求的文档:)

(披露:我在 MS 的 Python SDK 团队工作)

关于python - Azure数据工厂管道: Creating pipelines with Python: Authentication (via az cli),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55532568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com