跳转到主要内容

Microsoft Azure Purview Sharing Python客户端库

项目描述

Azure Purview Sharing Python客户端库

Microsoft Purview Share是一个完全托管的云服务。

请根据服务的文档和我们的协议客户端文档使用此库

源代码 | 包(PyPI) | 产品文档

入门

安装包

使用pip安装Azure Purview Sharing Python客户端库

pip install azure-purview-sharing

先决条件

  • 您必须有一个Azure订阅和一个Purview资源才能使用此包。
  • 使用此软件包需要 Python 3.6 或更高版本。

验证客户端

使用 Azure Active Directory

本文档演示了如何使用 DefaultAzureCredential 通过 Azure Active Directory 进行身份验证。然而,任何由 azure-identity 包 提供的凭证都将被接受。有关其他凭证的更多信息,请参阅 azure-identity 文档。

选择并配置您的凭证后,您可以创建 PurviewSharingClient 的实例。

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
client = PurviewSharingClient(endpoint="https://<my-account-name>.purview.azure.com", credential=credential)

关键概念

数据提供者:数据提供者是指通过选择数据源、选择要共享的文件和文件夹以及要与之共享的人来创建共享的个人。然后,Microsoft Purview 向每个数据消费者发送邀请。

数据消费者:数据消费者是指通过指定他们自己的 Azure 订阅中的目标存储帐户来接受邀请的个人,他们将在其中使用它来访问共享的数据。

示例

目录

数据提供者示例

以下代码示例演示了数据提供者如何使用 Microsoft Azure Python SDK for Purview Sharing 来管理他们的共享活动。

创建已发送的共享客户端

import os, uuid, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

创建共享

要开始共享数据,数据提供者必须首先创建一个已发送的共享,以标识他们想要共享的数据。

import os, uuid, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

sent_share_id = uuid.uuid4()

artifact = {
    "properties": {
        "paths": [
            {
                "containerName": "container-name",
                "receiverPath": "shared-file-name.txt",
                "senderPath": "original/file-name.txt"
            }
        ]
    },
    "storeKind": "AdlsGen2Account",
    "storeReference": {
        "referenceName": "/subscriptions/{subscription-id}/resourceGroups/provider-storage-rg/providers/Microsoft.Storage/storageAccounts/providerstorage",
        "type": "ArmResourceReference"
    }
}

sent_share = {
    "properties": {
        "artifact": artifact,
        "displayName": "sampleShare",
        "description": "A sample share"
    },
    "shareKind": "InPlace"
}

request = client.sent_shares.begin_create_or_replace(
    str(sent_share_id),
    sent_share=sent_share)

response = request.result()
print(response)

向用户发送共享邀请

创建已发送的共享后,数据提供者可以向消费者发送邀请,消费者可以查看共享的数据。在此示例中,通过指定电子邮件地址向个人发送了邀请。

import os, uuid

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential
from datetime import date

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

sent_share_id = uuid.uuid4()
sent_share_invitation_id = uuid.uuid4()

consumerEmail = "consumer@contoso.com"
today = date.today()
invitation = {
    "invitationKind": "User",
    "properties": {
        "targetEmail": consumerEmail,
        "notify": "true",
        "expirationDate": date(today.year+1,today.month,today.day).strftime("%Y-%m-%d") + " 00:00:00"
    }
}

invitation_request = client.sent_shares.create_invitation(
    sent_share_id=str(sent_share_id),
    sent_share_invitation_id=str(sent_share_invitation_id),
    sent_share_invitation=invitation)

invitation_response = invitation_request.result()
created_invitation = json.loads(invitation_response)
print(created_invitation)

向服务发送共享邀请

数据提供者还可以通过指定服务的租户 ID 和对象 ID 来向服务或应用程序发送邀请。用于向服务发送邀请的对象 ID 必须是与企业应用程序关联的对象 ID(而不是应用程序注册)。

import os, uuid

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

targetActiveDirectoryId = uuid.uuid4()
targetObjectId = uuid.uuid4()

sent_share_invitation = {
    "invitationKind": "Service",
    "properties": {
        "targetActiveDirectoryId": str(targetActiveDirectoryId),
        "targetObjectId": str(targetObjectId)
    }
}

invitation_response = client.sent_shares.create_invitation(
    sent_share_id=str(sent_share_id),
    sent_share_invitation_id=str(sent_share_invitation_id),
    sent_share_invitation=sent_share_invitation)

print(invitation_response)

获取已发送的共享

创建已发送的共享后,数据提供者可以检索它。

import os, uuid

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()
sent_share_id = uuid.uuid4()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

retrieved_sent_share = client.sent_shares.get(sent_share_id=str(sent_share_id))
print(retrieved_sent_share)

列出已发送的共享

数据提供者还可以检索他们创建的已发送共享的列表。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

provider_storage_account_resource_id = "/subscriptions/{subscription-id}/resourceGroups/provider-storage-rg/providers/Microsoft.Storage/storageAccounts/providerstorage"

list_request = client.sent_shares.list(
    reference_name=provider_storage_account_resource_id,
    orderby="properties/createdAt desc")

for list_response in list_request:
    print(list_response)

删除已发送的共享

数据提供者可以通过删除已发送的共享来停止与所有数据消费者共享他们的数据。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

sent_share_id = uuid.uuid4()

delete_request = client.sent_shares.begin_delete(sent_share_id=str(sent_share_id))
delete_response = delete_request.result()
print(delete_response)

获取已发送的共享邀请

创建已发送的共享邀请后,数据提供者可以检索它。

import os, uuid, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

sent_share_id = uuid.uuid4()
sent_share_invitation_id = uuid.uuid4()

get_invitation_response = client.sent_shares.get_invitation(
    sent_share_id=str(sent_share_id), 
    sent_share_invitation_id=str(sent_share_invitation_id))

retrieved_share_invitation = json.loads(get_invitation_response)
print(retrieved_share_invitation)

列出已发送的共享邀请

数据提供者还可以检索他们创建的已发送共享邀请的列表。

import os, uuid, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint, credential=credential)

sent_share_id = uuid.uuid4()

list_request = client.sent_shares.list_invitations(sent_share_id=str(sent_share_id))

for list_response in list_request:
    print(list_response)

删除已发送的共享邀请

数据提供者可以通过删除单个已发送的共享邀请来停止与特定数据消费者共享他们的数据。

import os, uuid, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

sent_share_id = uuid.uuid4()
sent_share_invitation_id = uuid.uuid4()

delete_invitation_request = client.sent_shares.begin_delete_invitation(
    sent_share_id=str(sent_share_id),
    sent_share_invitation_id=str(sent_share_invitation_id))
delete_invitation_response = delete_invitation_request.result()
print(delete_invitation_response)

数据消费者示例

以下代码示例演示了数据消费者如何使用 Microsoft Azure Python SDK for Purview Sharing 来管理他们的共享活动。

创建接收共享客户端

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

列出断开连接的接收共享

要开始查看与他们共享的数据,数据消费者必须首先检索断开连接的接收共享的列表。在此列表中,他们可以识别一个断开连接的接收共享以附加。一个“断开连接”的接收共享是指从未附加或已断开的接收共享。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

list_detached_response = client.received_shares.list_detached(orderby="properties/createdAt desc")
print(list_detached_response)

附加接收共享

一旦数据消费者识别了一个接收共享,他们可以将接收共享附加到他们可以访问共享数据的任何位置。如果接收共享已经附加,则共享数据将在新指定的位置提供访问。

import os, json

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

consumer_storage_account_resource_id = "/subscriptions/{subscription-id}/resourceGroups/consumer-storage-rg/providers/Microsoft.Storage/storageAccounts/consumerstorage"

list_detached_response = client.received_shares.list_detached(orderby="properties/createdAt desc")
received_share = next(x for x in list_detached_response)

store_reference = {
    "referenceName": consumer_storage_account_resource_id,
    "type": "ArmResourceReference"
}

sink = {
    "properties": {
        "containerName": "container-test",
        "folder": "folder-test",
        "mountPath": "mountPath-test",
    },
    "storeKind": "AdlsGen2Account",
    "storeReference": store_reference
}

received_share['properties']['sink'] = sink

update_request = client.received_shares.begin_create_or_replace(
    received_share['id'],
    content_type="application/json",
    content=json.dumps(received_share))

update_response = update_request.result()
print(update_response)

获取接收共享

数据消费者可以检索单个接收共享。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

list_detached_response = client.received_shares.list_detached(orderby="properties/createdAt desc")
list_detached = json.loads(list_detached_response)
received_share = list_detached[0]

get_share_response = client.received_shares.get(received_share_id=received_share['id'])
retrieved_share = json.loads(get_share_response)
print(retrieved_share)

列出附加的接收共享

数据消费者还可以检索他们附加的接收共享的列表。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

consumer_storage_account_resource_id = "/subscriptions/{subscription-id}/resourceGroups/consumer-storage-rg/providers/Microsoft.Storage/storageAccounts/consumerstorage"

list_attached_response = client.received_shares.list_attached(
    reference_name=consumer_storage_account_resource_id,
    orderby="properties/createdAt desc")
print(list_attached_response)

删除接收共享

接收到的共享可以被数据消费者删除,以终止他们对共享数据的访问。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

delete_received_share_request = client.received_shares.begin_delete(received_share_id=received_share['id'])
delete_received_share_response = delete_received_share_request.result()
print(delete_received_share_response)

共享资源示例

以下代码示例演示了如何使用Microsoft Azure Python SDK for Purview Sharing来查看共享资源。共享资源是提供者共享数据的基础资源,或者消费者附加共享数据的目标。

列出共享资源

可以检索共享资源列表,以查看在账户中发生共享活动的所有资源。

import os

from azure.purview.sharing import PurviewSharingClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["ENDPOINT"]
credential = DefaultAzureCredential()

client = PurviewSharingClient(endpoint=endpoint,credential=credential)

list_request = client.share_resources.list(
    filter="properties/storeKind eq 'AdlsGen2Account'",
    orderby="properties/createdAt desc")

for list_response in list_request:
    print(list_response)

故障排除

通用

如果你在响应上调用 .raise_for_status(),Purview 目录客户端将引发在 Azure Core 中定义的异常。

日志记录

此库使用标准的 logging 库进行日志记录。HTTP会话(URL、标头等)的基本信息以INFO级别进行记录。

可以通过具有 logging_enable 关键字参数的客户端启用详细DEBUG级别日志记录,包括请求/响应体和未删除的标头。

import sys
import logging
from azure.identity import DefaultAzureCredential
from azure.purview.sharing import PurviewSharingClient

# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

endpoint = "https://<my-account-name>.share.purview.azure.com"
credential = DefaultAzureCredential()

# This client will log detailed information about its HTTP sessions, at DEBUG level
client = PurviewSharingClient(endpoint=endpoint, credential=credential, logging_enable=True)

同样,即使没有为客户端启用,logging_enable 也可以为单个 send_request 调用启用详细日志记录。

result = client.types.get_all_type_definitions(logging_enable=True)

下一步

有关更多通用示例,请参阅我们的 示例

贡献

此项目欢迎贡献和建议。大多数贡献需要你同意一份贡献者许可协议(CLA),声明你有权,实际上确实授予我们使用你的贡献的权利。有关详细信息,请访问 cla.microsoft.com

当你提交拉取请求时,CLA-bot 将自动确定你是否需要提供 CLA,并相应地装饰 PR(例如,标签、注释)。只需遵循机器人提供的说明即可。你只需在所有使用我们 CLA 的存储库中这样做一次。

此项目已采用 Microsoft 开源行为准则。有关更多信息,请参阅 行为准则常见问题解答 或联系 opencode@microsoft.com 了解任何其他问题或评论。

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解有关 安装软件包 的更多信息。

源分布

azure-purview-sharing-1.0.0b3.zip (95.5 kB 查看哈希值)

上传时间

构建分布

azure_purview_sharing-1.0.0b3-py3-none-any.whl (67.3 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面