快速入门:使用 Microsoft Purview 数据共享 .NET SDK 共享和接收数据

重要

此功能目前处于预览阶段。 Microsoft Azure 预览版补充使用条款包括适用于 Beta 版、预览版或其他尚未正式发布的 Azure 功能的其他法律条款。

在本快速入门中,你将使用 .NET SDK 共享数据和接收来自 Azure Data Lake Storage (ADLS Gen2) 或 Blob 存储帐户的共享。 本文包含代码片段,可用于使用 Microsoft Purview 数据共享 创建、接受和管理共享。

有关数据共享工作原理的概述,watch此简短演示

注意

此功能已在 2023 年 2 月更新,在 Microsoft Purview 中查看和管理数据共享所需的 SDK 和权限已更改。

  • Microsoft Purview 现在无需任何权限即可使用 SDK 创建和管理共享。 (读取者权限才能使用 Microsoft Purview 治理门户共享数据。)
  • 存储帐户仍需要权限。

请参阅更新的 NuGet 包更新的代码片段 以使用更新的 SDK。

先决条件

Microsoft Purview 先决条件

  • Microsoft Purview 帐户。 还可以使用两个 Microsoft Purview 帐户,一个用于数据提供程序,另一个用于数据使用者,以测试这两种方案。
  • 可用于向其发送邀请的收件人的 Azure 登录电子邮件地址。 收件人的电子邮件别名不起作用。

Azure 存储帐户先决条件

  • Azure 订阅必须注册 AllowDataSharing 预览版功能。 使用 Azure 门户 或 PowerShell 执行以下步骤。

    1. 在“Azure 门户”中,选择用于创建源和目标存储帐户的 Azure 订阅。
    2. 在左侧菜单中,选择“设置”下的“预览功能”。
    3. 选择 “AllowDataSharing”“注册”。
    4. 刷新 “预览功能 ”屏幕,验证 状态 是否为 “已注册”。 完成注册可能需要 15 分钟到 1 小时。
    5. 此外,若要将数据共享用于美国东部、美国东部 2、北欧、美国中南部、美国中西部、西欧、美国西部、美国西部 2、美国西部 3 的存储帐户,请选择 AllowDataSharingInHeroRegion 和 Register

    有关详细信息,请参阅 注册预览功能

    RegistrationState 应为 Registered。 完成注册可能需要 15 分钟到 1 小时。 有关详细信息,请参阅 注册预览功能

    注意

    以下是支持的存储帐户配置:

    • Azure 区域:加拿大中部、加拿大东部、英国南部、英国西部、澳大利亚东部、日本东部、韩国南部和南非北部
    • 其他 Azure 区域:美国东部、美国东部 2、北欧、美国中南部、美国中西部、西欧、美国西部、美国西部 2、美国西部 3
    • 性能:标准
    • 冗余选项:LRS
  • 完成注册步骤 后创建的 源和目标存储帐户。 两个存储帐户必须彼此位于同一 Azure 区域中。 这两个存储帐户都需要是 ADLS Gen2 或 Blob 存储帐户。 存储帐户可以位于与 Microsoft Purview 帐户不同的 Azure 区域。

  • 最新版本的存储 SDK、PowerShell、CLI 和 Azure 存储资源管理器。 存储 REST API 版本必须为 2020 年 2 月或更高版本。

  • 存储帐户需要在要发送或接收共享的集合中注册。 如果使用一个 Microsoft Purview 帐户,这可以是两个不同的集合,也可以是同一个集合。 有关注册的说明,请参阅 ADLS Gen2Blob 存储 数据源页。

  • 如果源或目标存储帐户与 Microsoft Purview 帐户的订阅位于不同的 Azure 订阅中,则当共享提供程序添加资产或共享使用者映射资产时,Microsoft.Purview 资源提供程序会自动在数据存储所在的 Azure 订阅中注册, 并且仅 当用户有权为资源提供程序执行 /register/action 操作时。 该权限包含在“参与者”和“所有者”角色中。

    注意

    仅当首次将数据共享或接收到 Azure 订阅中的存储帐户时,才需要此注册。

所需角色

下面是共享数据和接收共享所需的角色。

Azure 存储帐户角色 Microsoft Purview 集合角色
数据提供程序 以下角色之一:
  • 所有者
  • 存储 Blob 数据所有者
数据读取器
数据使用者 以下角色之一:
  • 参与者
  • 所有者
  • 存储 Blob 数据参与者
  • 存储 Blob 数据所有者
数据读取器

注意

如果创建了 Microsoft Purview 帐户,系统会自动将所有角色分配给根集合。 若要详细了解 Microsoft Purview 集合和角色,请参阅 Microsoft Purview 权限。

Visual Studio

本文中的演练使用 Visual Studio 2022。 Visual Studio 2013、2015、2017 或 2019 的过程可能略有不同。

Azure .NET SDK

在计算机上下载并安装 Azure .NET SDK

使用服务主体

在本教程的代码片段中,可以使用自己的凭据或服务主体进行身份验证。 若要设置服务主体,请按照以下说明操作:

  1. “创建 Azure Active Directory 应用程序”中,创建一个代表在本教程中创建的 .NET 应用程序的应用程序。 对于登录 URL,可以提供虚拟 URL,如文章 () https://contoso.org/exampleapp 所示。

  2. “获取用于登录的值”中,获取 应用程序 ID租户 ID对象 ID,并记下本教程稍后使用的这些值。

  3. “证书和机密”中,获取 身份验证密钥,并记下本教程稍后使用的此值。

  4. 将应用程序分配到以下角色:

    User Azure 存储帐户角色 Microsoft Purview 集合角色
    数据提供程序 以下角色之一:
    • 所有者
    • Blob 存储数据所有者
    Data Share参与者
    数据使用者 以下角色之一:
    • 参与者
    • 所有者
    • 存储 Blob 数据参与者
    • Blob 存储数据所有者
    Data Share参与者

创建 Visual Studio 项目

接下来,在 Visual Studio 中创建 C# .NET 控制台应用程序:

  1. 启动 Visual Studio
  2. 在“开始”窗口中,选择“ 新建项目>控制台应用”。 需要 .NET 6.0 或更高版本。
  3. “项目名称”中,输入 PurviewDataSharingQuickStart
  4. 选择“ 创建 ”以创建项目。

安装 NuGet 包

  1. 选择 “工具”>“NuGet 包管理器>包管理器控制台”。

  2. 包管理控制台中 ,运行此页上显示的 .NET cli add package 命令以添加 NuGet 包: Microsoft.Azure.Analytics.Purview.Sharing NuGet 包

  3. “包管理器控制台 ”窗格中,运行以下命令以安装包。

    Install-Package Azure.Analytics.Purview.Sharing -IncludePrerelease
    Install-Package Azure.Identity
    

    提示

    如果收到“找不到任何项目...”的错误尝试使用这些命令时,可能只需将项目中的文件夹级别下移。 尝试使用 命令dir列出目录中的文件夹,然后使用“项目文件夹>的 cd <名称”将级别向下移动到项目文件夹。 然后重试。

创建已发送的共享

此脚本创建可发送给内部或外部用户的数据共享。 若要使用它,请务必填写以下变量:

  • SenderTenantId - 发件人标识的 Azure 租户 ID
  • SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
  • ShareName - 已发送共享的显示名称。
  • ShareDescription - (可选) 已发送共享的说明。
  • SenderStorageKind - BlobAccount 或 AdlsGen2Account。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • SenderStorageContainer - 存储要共享数据的容器的名称。
  • SenderPathToShare - 要共享的数据的文件/文件夹路径。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • SentShareID - (可选) 此选项必须是 GUID,并且当前值会为你生成一个,但你可以根据需要将其替换为其他值。
  • ReceiverVisiblePath - (可选) 接收方将看到的共享的名称。 当前设置为 GUID,但不需要 GUID。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string SenderTenantId = "<Sender Identity's Tenant ID>";
    private static string SenderPurviewAccountName = "<Sender Purview Account Name>";

    private static string ShareName = "<Share Display Name>";
    private static string ShareDescription = "Share created using the SDK.";
    private static string SenderStorageKind = "<Sender Storage Account Kind (BlobAccount / AdlsGen2Account)>";
    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
    private static string SenderStorageContainer = "<Share Data Container Name>";
    private static string SenderPathToShare = "<File/Folder Path To Share>";

    // Set if using Service principal to create shares
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // [OPTIONAL INPUTS] Override Value If Desired.
    private static string SentShareId = Guid.NewGuid().ToString();
    private static string ReceiverVisiblePath = Guid.NewGuid().ToString();

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            /// Replace all placeholder inputs above with actual values before running this program.
            /// This updated Share experience API will create Shares based on callers RBAC role on the storage account.
            /// To view/manage Shares via UX in Purview Studio. Storage accounts need to be registered (one time action) in Purview account with DSA permissions.

            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateShare - START");
            await Sender_CreateSentShare();
            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateShare - FINISH");
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    private static async Task<BinaryData> Sender_CreateSentShare()
    {

        TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

        SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

        if (sentSharesClient == null)
        {
            throw new InvalidEnumArgumentException("Invalid Sent Shares Client.");
        }

        // Create sent share
        var inPlaceSentShareDto = new
        {
            shareKind = "InPlace",
            properties = new
            {
                displayName = ShareName,
                description = ShareDescription,
                artifact = new
                {
                    storeKind = SenderStorageKind,
                    storeReference = new
                    {
                        referenceName = SenderStorageResourceId,
                        type = "ArmResourceReference"
                    },
                    properties = new
                    {
                        paths = new[]
                        {
                            new
                            {
                                receiverPath = ReceiverVisiblePath,
                                containerName = SenderStorageContainer,
                                senderPath = SenderPathToShare
                            }
                        }
                    }
                }
            },
        };

        Operation<BinaryData> sentShare = await sentSharesClient.CreateOrReplaceSentShareAsync(WaitUntil.Completed, SentShareId, RequestContent.Create(inPlaceSentShareDto));
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(sentShare.Value);
        Console.ForegroundColor = Console.ForegroundColor;
        return sentShare.Value;
    }
}

向用户发送邀请

此脚本向用户发送共享的电子邮件邀请。 如果要向服务主体发送邀请, 请参阅下一个代码示例。 若要使用它,请务必填写以下变量:

  • RecipientUserEmailId - Email用户向其发送邀请的地址。
  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • SentShareDisplayName - 要为其发送邀请的已发送共享的名称。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • InvitationId - (可选) 此选项必须是 GUID,并且当前值会为你生成一个,但你可以根据需要将其替换为其他值。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string RecipientUserEmailId = "<Target User's Email Id>";

    private static string SenderTenantId = "<Sender Indentity's Tenant ID>";
    private static string SenderPurviewAccountName = "<Sender Purview Account Name>";
    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";

    // Set if using Service principal to send invitation
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    private static string SentShareDisplayName = "<Name of share you're sending an invite for.>";
    private static string InvitationId = Guid.NewGuid().ToString();

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
    private static int StepCounter = 0;

    private static async Task Main(string[] args)
    {
        try
        {

            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: SendtoUser - START");
            await Sender_CreateUserRecipient();
            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: SendtoUser - FINISH");
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }

    private static async Task<BinaryData> Sender_CreateUserRecipient()
    {

        TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

        SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

        if (string.IsNullOrEmpty(RecipientUserEmailId))
        {
            throw new InvalidEnumArgumentException("Invalid Recipient User Email Id.");
        }

        // Create user recipient and invite
        var invitationData = new
        {
            invitationKind = "User",
            properties = new
            {
                expirationDate = DateTime.Now.AddDays(7).ToString(),
                notify = true, // Send invitation email
                targetEmail = RecipientUserEmailId
            }
        };

        var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();

        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("{0}. {1}...", ++StepCounter, "Get a Specific Sent Share");
        Console.ForegroundColor = Console.ForegroundColor;

        var mySentShare = allSentShares.First(sentShareDoc =>
        {
            var doc = JsonDocument.Parse(sentShareDoc).RootElement;
            var props = doc.GetProperty("properties");
            return props.GetProperty("displayName").ToString() == SentShareDisplayName;
        });

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("My Sent Share Id: " + JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString());
        Console.ForegroundColor = Console.ForegroundColor;

        var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();

        var sentInvitation = await sentSharesClient.CreateSentShareInvitationAsync(SentShareId, InvitationId, RequestContent.Create(invitationData));

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(sentInvitation.Content);
        Console.ForegroundColor = Console.ForegroundColor;

        return sentInvitation.Content;
    }
}

向服务发送邀请

此脚本向服务主体发送共享的电子邮件邀请。 如果要向用户发送邀请, 请参阅上一示例。 若要使用它,请务必填写以下变量:

  • RecipientApplicationTenantId - 接收服务主体的 Azure 租户 ID。
  • RecipientApplicationObjectId - 接收服务主体的对象 ID。
  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • SentShareDisplayName - 要为其发送邀请的已发送共享的名称。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • InvitationId - (可选) 此选项必须是 GUID,并且当前值会为你生成一个,但你可以根据需要将其替换为其他值。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string RecipientApplicationTenantId = "<Target Application's Tenant Id>";
    private static string RecipientApplicationObjectId = "<Target Application's Object Id>";

    private static string SentShareDisplayName = "<Name of share you're sending an invite for.>";
    private static string InvitationId = Guid.NewGuid().ToString();

    private static string SenderTenantId = "<Sender Indentity's Tenant ID>";
    private static string SenderPurviewAccountName = "<Sender Purview Account Name>";

    private static string SenderStorageResourceId = "<Resource ID for storage account that has been shared>";

    // Set if using Service principal to send invitation
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {

            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: SendtoService - START");
            await Sender_CreateServiceRecipient();
            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: SendtoService - FINISH");
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }

    private static async Task<BinaryData> Sender_CreateServiceRecipient()
    {

        TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

        SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

        if (!Guid.TryParse(RecipientApplicationTenantId, out Guid _))
        {
            throw new InvalidEnumArgumentException("Invalid Recipient Service Tenant Id.");
        }

        if (!Guid.TryParse(RecipientApplicationObjectId, out Guid _))
        {
            throw new InvalidEnumArgumentException("Invalid Recipient Service Object Id.");
        }

        // Create service recipient
        var invitationData = new
        {
            invitationKind = "Service",
            properties = new
            {
                expirationDate = DateTime.Now.AddDays(5).ToString(),
                targetActiveDirectoryId = RecipientApplicationTenantId,
                targetObjectId = RecipientApplicationObjectId
            }
        };


        var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
        var mySentShare = allSentShares.First(sentShareDoc =>
        {
            var doc = JsonDocument.Parse(sentShareDoc).RootElement;
            var props = doc.GetProperty("properties");
            return props.GetProperty("displayName").ToString() == SentShareDisplayName;
        });

        var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();

        var sentInvitation = await sentSharesClient.CreateSentShareInvitationAsync(SentShareId, InvitationId, RequestContent.Create(invitationData));
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(sentInvitation.Content);
        Console.ForegroundColor = Console.ForegroundColor;
        return sentInvitation.Content;
    }

}

列出已发送的共享

此脚本列出了特定存储资源的所有已发送共享。 若要使用它,请务必填写以下变量:

  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
  • SenderStorageResourceId - 从中发送共享 的存储帐户的资源 ID
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string SenderTenantId = "<Sender Tenant ID>";
    private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";

    // Set if using Service principal to list shares
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {


        try
        {
            TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

            SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

            var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
            Console.WriteLine(allSentShares);
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

列出所有共享收件人

此脚本列出特定共享的所有收件人。 若要使用它,请务必填写以下变量:

  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
  • SentShareDisplayName - 要为其列出收件人的已发送共享的名称。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string SentShareDisplayName = "<Name of share you're listing recipients for.>";
    private static string SenderTenantId = "<Sender Tenant ID>";
    private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";

    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";

    // Set if using Service principal to list recipients
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

            SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

            var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
            var mySentShare = allSentShares.First(sentShareDoc =>
            {
                var doc = JsonDocument.Parse(sentShareDoc).RootElement;
                var props = doc.GetProperty("properties");
                return props.GetProperty("displayName").ToString() == SentShareDisplayName;
            });

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("My Sent Share Id: " + JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString());
            Console.ForegroundColor = Console.ForegroundColor;

            var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();

            var allRecipients = await sentSharesClient.GetAllSentShareInvitationsAsync(SentShareId).ToResultList();
            Console.WriteLine(allRecipients);

        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

删除收件人

此脚本删除收件人的共享邀请,因此删除共享。 若要使用它,请务必填写以下变量:

  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
  • SentShareDisplayName - 要删除收件人的已发送共享的名称。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • RecipientUserEmailId - Email要删除的用户的地址。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string SentShareDisplayName = "<Name of share you're removing a recipient for.>";
    private static string SenderTenantId = "<Sender Tenant ID>";
    private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
    private static string RecipientUserEmailId = "<Target User's Email Id>";

    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";

    // Set if using Service principal to delete recipients
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

            SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

            var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();

            var mySentShare = allSentShares.First(sentShareDoc =>
            {
                var doc = JsonDocument.Parse(sentShareDoc).RootElement;
                var props = doc.GetProperty("properties");
                return props.GetProperty("displayName").ToString() == SentShareDisplayName;
            });

            var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();

            var allRecipients = await sentSharesClient.GetAllSentShareInvitationsAsync(SentShareId).ToResultList();

            var recipient = allRecipients.First(recipient =>
            {
                var doc = JsonDocument.Parse(recipient).RootElement;
                var props = doc.GetProperty("properties");
                return props.TryGetProperty("targetEmail", out JsonElement rcpt) && rcpt.ToString() == RecipientUserEmailId;
            });

            var recipientId = JsonDocument.Parse(recipient).RootElement.GetProperty("id").ToString();

            await sentSharesClient.DeleteSentShareInvitationAsync(WaitUntil.Completed, SentShareId, recipientId);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Remove Id: " + JsonDocument.Parse(recipient).RootElement.GetProperty("id").ToString());
            Console.WriteLine("Complete");
            Console.ForegroundColor = Console.ForegroundColor;

        }

        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

删除已发送的共享

此脚本删除已发送的共享。 若要使用它,请务必填写以下变量:

  • SenderTenantId - 共享发件人标识的 Azure 租户 ID。
  • SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
  • SentShareDisplayName - 要为其列出收件人的已发送共享的名称。
  • SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true
  • SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
  • SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端机密/身份验证密钥。
  • SenderPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{SenderPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string SenderTenantId = "<Sender Tenant ID>";
    private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
    private static string SentShareDisplayName = "<Name of share you're removing.>";

    private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";

    // Set if using Service principal to delete share
    private static bool UseServiceTokenCredentials = false;
    private static string SenderClientId = "<Sender Application (Client) Id>";
    private static string SenderClientSecret = "<Sender Application (Client) Secret>";

    // General Configs
    private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
    private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            TokenCredential senderCredentials = UseServiceTokenCredentials
                ? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
                : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });

            SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);

            var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();

            var mySentShare = allSentShares.First(sentShareDoc =>
            {
                var doc = JsonDocument.Parse(sentShareDoc).RootElement;
                var props = doc.GetProperty("properties");
                return props.GetProperty("displayName").ToString() == SentShareDisplayName;
            });

            var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();

            await sentSharesClient.DeleteSentShareAsync(WaitUntil.Completed, SentShareId);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Remove Id: " + SentShareId);
            Console.WriteLine("Complete");
            Console.ForegroundColor = Console.ForegroundColor;

        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

创建收到的共享

此脚本允许你接收数据共享。 若要使用它,请务必填写以下变量:

  • ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
  • ReceiverPurviewAccountName - 接收数据的 Microsoft Purview 帐户的名称。
  • ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
  • ReceiverStorageResourceId - 将接收数据的 存储帐户的资源 ID
  • ReceiverStorageContainer - 将存储共享数据的容器的名称。
  • ReceiverTargetFolderName - 共享数据的存储位置的文件夹路径。
  • ReceiverTargetMountPath - 你想要用于将数据存储在文件夹中的装载路径。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体接收共享,请将此项设置为 true
  • ReceiverClientId - (可选) 如果使用服务主体接收共享,这是服务主体的应用程序 (客户端) ID。
  • ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端机密/身份验证密钥。
  • ReceivedShareId - (可选) 此选项必须是 GUID,并且当前值将为你生成一个,但你可以根据需要将其替换为其他值。
  • ReceiverVisiblePath - (要用于已接收共享的路径的可选) 名称。
  • ReceivedShareDisplayName - (可选) 接收共享的显示名称。
  • ReceiverPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{ReceiverPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
    private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";

    private static string ReceiverStorageKind = "<Receiver Storage Account Kind (BlobAccount / AdlsGen2Account)>";
    private static string ReceiverStorageResourceId = "<Receiver Storage Account Resource Id>";
    private static string ReceiverStorageContainer = "<Container Name To Receive Data Under>";
    private static string ReceiverTargetFolderName = "<Folder Name to Received Data Under>";
    private static string ReceiverTargetMountPath = "<Mount Path to store Received Data Under>";

    //Use if using a service principal to receive a share
    private static bool UseServiceTokenCredentials = false;
    private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
    private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";

    // [OPTIONAL INPUTS] Override Values If Desired.
    private static string ReceivedShareId = Guid.NewGuid().ToString();
    private static string ReceiverVisiblePath = "ReceivedSharePath";

    private static string ReceivedShareDisplayName = "ReceivedShare";

    // General Configs
    private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
    private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {


            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateReceivedShare - START");
            await Receiver_CreateReceivedShare();
            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateReceivedShare - FINISH");
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    private static async Task<BinaryData> Receiver_CreateReceivedShare()
    {

        TokenCredential receiverCredentials = UseServiceTokenCredentials
            ? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
            : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });

        ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);

        if (receivedSharesClient == null)
        {
            throw new InvalidEnumArgumentException("Invalid Received Shares Client.");
        }

        var results = await receivedSharesClient.GetAllDetachedReceivedSharesAsync().ToResultList();
        var detachedReceivedShare = results;

        if (detachedReceivedShare == null)
        {
            throw new InvalidOperationException("No received shares found.");
        }



        var myReceivedShare = detachedReceivedShare.First(recShareDoc =>
        {
            var doc = JsonDocument.Parse(recShareDoc).RootElement;
            var props = doc.GetProperty("properties");
            return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
        });

        var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();


        var attachedReceivedShareData = new
        {
            shareKind = "InPlace",
            properties = new
            {
                displayName = ReceivedShareDisplayName,
                sink = new
                {
                    storeKind = ReceiverStorageKind,
                    properties = new
                    {
                        containerName = ReceiverStorageContainer,
                        folder = ReceiverTargetFolderName,
                        mountPath = ReceiverTargetMountPath
                    },
                    storeReference = new
                    {
                        referenceName = ReceiverStorageResourceId,
                        type = "ArmResourceReference"
                    }
                }
            }
        };

        var receivedShare = await receivedSharesClient.CreateOrReplaceReceivedShareAsync(WaitUntil.Completed, ReceivedShareId, RequestContent.Create(attachedReceivedShareData));

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(receivedShare.Value);
        Console.ForegroundColor = Console.ForegroundColor;

        return receivedShare.Value;
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

列出所有收到的共享

此脚本列出存储帐户上收到的所有共享。 若要使用它,请务必填写以下变量:

  • ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
  • ReceiverPurviewAccountName - 接收数据的 Microsoft Purview 帐户的名称。
  • ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体接收共享,请将此项设置为 true
  • ReceiverClientId - (可选) 如果使用服务主体接收共享,这是服务主体的应用程序 (客户端) ID。
  • ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端机密/身份验证密钥。
  • ReceiverPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{ReceiverPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
    private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";

    private static string ReceiverStorageResourceId = "<Storage Account Resource Id that is housing shares>";

    //Use if using a service principal to list shares
    private static bool UseServiceTokenCredentials = false;
    private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
    private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";

    // General Configs
    private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
    private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            TokenCredential receiverCredentials = UseServiceTokenCredentials
            ? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
            : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });

            ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);

            var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();
            Console.WriteLine(allReceivedShares);
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

更新收到的共享

此脚本允许更新已接收共享的存储位置。 就像创建接收的共享一样,为要在其中存储数据的存储帐户添加信息。 若要使用它,请务必填写以下变量:

  • ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
  • ReceiverPurviewAccountName - 接收数据的 Microsoft Purview 帐户的名称。
  • ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
  • ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID
  • ReAttachStorageResourceId - 将接收数据的 存储帐户的资源 ID
  • ReceiverStorageContainer - 将存储共享数据的容器的名称。
  • ReceiverTargetFolderName - 共享数据的存储位置的文件夹路径。
  • ReceiverTargetMountPath - 你想要用于将数据存储在文件夹中的装载路径。
  • ReceivedShareDisplayName - 收到的共享的显示名称。
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体接收共享,请将此项设置为 true
  • ReceiverClientId - (可选) 如果使用服务主体接收共享,这是服务主体的应用程序 (客户端) ID。
  • ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端机密/身份验证密钥。
  • ReceiverPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{ReceiverPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
    private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";

    private static string ReceiverStorageKind = "<Receiver Storage Account Kind (BlobAccount / AdlsGen2Account)>";
    private static string ReceiverStorageResourceId = "<Storage Account Resource Id for the account where the share is currently attached.>";
    private static string ReAttachStorageResourceId = "<Storage Account Resource Id For Reattaching Received Share>";
    private static string ReceiverStorageContainer = "<Container Name To Receive Data Under>";
    private static string ReceiverTargetFolderName = "<Folder Name to Received Data Under>";
    private static string ReceiverTargetMountPath = "<Mount Path to Received Data Under>";

    private static string ReceivedShareDisplayName = "<Display name of your received share>";

    //Use if using a service principal to update the share
    private static bool UseServiceTokenCredentials = false;
    private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
    private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";

    // General Configs
    private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
    private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {

            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: UpdateReceivedShare - START");
            await Receiver_UpdateReceivedShare();
            Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: UpdateReceivedShare - FINISH");
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }

    private static async Task<BinaryData> Receiver_UpdateReceivedShare()
    {

        TokenCredential receiverCredentials = UseServiceTokenCredentials
            ? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
            : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });

        ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);

        if (receivedSharesClient == null)
        {
            throw new InvalidEnumArgumentException("Invalid Received Shares Client.");
        }

        var attachedReceivedShareData = new
        {
            shareKind = "InPlace",
            properties = new
            {
                displayName = ReceivedShareDisplayName,
                sink = new
                {
                    storeKind = ReceiverStorageKind,
                    properties = new
                    {
                        containerName = ReceiverStorageContainer,
                        folder = ReceiverTargetFolderName,
                        mountPath = ReceiverTargetMountPath
                    },
                    storeReference = new
                    {
                        referenceName = ReAttachStorageResourceId,
                        type = "ArmResourceReference"
                    }
                }
            }
        };

        var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();

        var myReceivedShare = allReceivedShares.First(recShareDoc =>
        {
            var doc = JsonDocument.Parse(recShareDoc).RootElement;
            var props = doc.GetProperty("properties");
            return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
        });

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("My Received Share Id: " + JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString());
        Console.ForegroundColor = Console.ForegroundColor;


        var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();

        var receivedShare = await receivedSharesClient.CreateOrReplaceReceivedShareAsync(WaitUntil.Completed, ReceivedShareId, RequestContent.Create(attachedReceivedShareData));

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(receivedShare.Value);
        Console.ForegroundColor = Console.ForegroundColor;

        return receivedShare.Value;
    }

    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

删除收到的共享

此脚本删除收到的共享。 若要使用它,请务必填写以下变量:

  • ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
  • ReceiverPurviewAccountName - 接收数据的 Microsoft Purview 帐户的名称。
  • ReceivedShareDisplayName - 收到的共享的显示名称。
  • ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID
  • UseServiceTokenCredentials - (可选) 如果要使用服务主体接收共享,请将此项设置为 true
  • ReceiverClientId - (可选) 如果使用服务主体接收共享,这是服务主体的应用程序 (客户端) ID。
  • ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端机密/身份验证密钥。
  • ReceiverPurviewEndPoint - 如果使用 经典 Microsoft purview 体验,请使用 $"https://{ReceiverPurviewAccountName}.purview.azure.com";如果你使用的是 新的 Microsoft Purview 体验,请使用 https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;

public static class PurviewDataSharingQuickStart
{
    // [REQUIRED INPUTS] Set To Actual Values.
    private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
    private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";

    private static string ReceivedShareDisplayName = "<Display name of your received share>";

    private static string ReceiverStorageResourceId = "<Storage Account Resource Id for the account where the share is currently attached.>";

    //Use if using a service principal to delete share.
    private static bool UseServiceTokenCredentials = false;
    private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
    private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";

    // General Configs
    private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
    private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";

    private static async Task Main(string[] args)
    {
        try
        {
            TokenCredential receiverCredentials = UseServiceTokenCredentials
            ? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
            : new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });

            ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);

            var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();

            var myReceivedShare = allReceivedShares.First(recShareDoc =>
            {
                var doc = JsonDocument.Parse(recShareDoc).RootElement;
                var props = doc.GetProperty("properties");
                return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
            });

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("My Received Share Id: " + JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString());
            Console.ForegroundColor = Console.ForegroundColor;

            var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();

            await receivedSharesClient.DeleteReceivedShareAsync(WaitUntil.Completed, ReceivedShareId);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Delete Complete");
            Console.ForegroundColor = Console.ForegroundColor;
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex);
            Console.ForegroundColor = Console.ForegroundColor;
        }
    }
    public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
    {
        List<T> list = new List<T>();

        await foreach (T item in asyncPageable)
        {
            list.Add(item);
        }

        return list;
    }
}

清理资源

若要清理为快速入门创建的资源,请使用以下准则:

  1. 在 Microsoft Purview 门户中, 删除已发送的共享
  2. 同时 删除收到的共享
  3. 成功删除共享后,请在收到共享数据时删除目标容器和 Microsoft Purview 在目标存储帐户中创建的文件夹。

后续步骤