无法使用 python 创建 azure 链接服务

Can't create azure linked service with python

我想做什么

我正在尝试创建一个 azure 数据工厂管道来将文件从 blob 复制并粘贴到 blob 或从 blob 到数据湖。

我做了什么

我刚刚按照 Microsoft 网站上的教程进行操作,我已经有一个 ADF 和 blob,所以我没有再次创建它,我现在正在尝试创建一个 Azure 链接服务,所以我使用了这段代码:

# Create an Azure Storage linked service
ls_name = 'storageLinkedService'

# IMPORTANT: specify the name and key of your Azure Storage account.
storage_string = SecureString('DefaultEndpointsProtocol=https;AccountName=<storageaccountname>;AccountKey=<storageaccountkey>')

ls_azure_storage = AzureStorageLinkedService(connection_string=storage_string)
ls = adf_client.linked_services.create_or_update(rg_name, df_name, ls_name, ls_azure_storage)
print_item(ls)

但是当我 运行 它时,我得到这个错误:

Traceback (most recent call last):

  File "<ipython-input-173-cabc65dd11b9>", line 4, in <module>
    ls = adf_client.linked_services.create_or_update(rg_name, df_name, ls_name, ls_azure_storage)

  File "C:\ProgramData\Anaconda3\lib\site-packages\azure\mgmt\datafactory\operations\linked_services_operations.py", line 170, in create_or_update
    raise models.ErrorResponseException(self._deserialize, response)

ErrorResponseException: Operation returned an invalid status code 'Not Found'

我不知道为什么会出现此错误。 有人有想法吗?

更新

我试图跳过这一步并尝试使用以下命令在 blob 中创建和归档:adf_client.datasets.create_or_update(rg_name, df_name, ds_name, ds_azure_blob)

我遇到了同样的错误 ErrorResponseException: Operation returned an invalid status code 'Not Found' 这让我认为问题可能与 blob 有关。

我试图重现您的问题但失败了。

我没有ADF所以按照官方教程创建了Data Factory和Linked service,没有出现错误。

主要功能

def main():

    # Azure subscription ID
    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 = '***'

    # The data factory name. It must be globally unique.
    df_name = '***'

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

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

    # create the resource group
    # comment out if the resource group already exits
     resource_client.resource_groups.create_or_update(rg_name, rg_params)

     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)

    # Create an Azure Storage linked service
    ls_name = 'storageLinkedService'

    # IMPORTANT: specify the name and key of your Azure Storage account.
    storage_string = SecureString('DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***')

    ls_azure_storage = AzureStorageLinkedService(connection_string=storage_string)
    ls = adf_client.linked_services.create_or_update(rg_name, df_name, ls_name, ls_azure_storage)
    print_item(ls)

ErrorResponseException: Operation returned an invalid status code 'Not Found'

我认为您可以检查参数是否有误或服务是否存在。(df、资源组、存储帐户)

如有任何疑问,请随时告诉我。


更新答案:

我在门户上的 Azure AD 中创建了自己的应用程序注册。

下面的应用程序 ID 是您的 client_id

创建你的secret并记得记下来,因为它稍后会隐藏。

下面的目录ID是tenant ID


更新答案2:

我也是贡献者角色,所以我认为这不是角色问题。 我试图将参数中的 ra_name(资源组) 更改为不存在的值,成功重现了你的异常。

请再次检查您的资源组或其他参数是否存在。


仅作总结:

ADF V1和V2目前好像不支持西欧地区

但是,article中的支持的地区章节尚未更新。困扰你这么久。您可以重新创建 ADF 并按照原始代码创建 link 服务。