快速入門:使用 Microsoft Purview Data Sharing .NET SDK 共用和接收資料

重要事項

僅淘汰 Microsoft Purview 資料控管功能

Microsoft Purview 統一目錄和傳統 Microsoft Purview 資料控管功能 (先前的 Azure Purview) 即將淘汰。 整個 Microsoft Purview 並未淘汰。 Microsoft Purview 資料安全性與合規性功能不受此公告影響,且 Microsoft 會繼續投資於此功能。

Microsoft 正在 Microsoft Fabric 中合併 其資料控管 功能。 無法再建立新的 Microsoft Purview 資料控管帳戶,而且現有的資料控管功能將維持在維護模式,直到 2027 年 11 月 12 日為止。

如需淘汰時間表、轉換指引,以及建議的後續步驟,請參閱 Microsoft Purview 資料控管淘汰公告。

注意事項

Microsoft Purview 資料目錄 (傳統) 、Data Health Insights (傳統) ,以及 Purview 工作流程 (傳統) 不再接受新客戶,這些服務先前為 Azure Purview 現在處於客戶支援模式。

重要事項

此功能目前已淘汰,但支援到 2025 年 9 月為止,以便移轉至 Microsoft Fabric 外部資料共用。

在本快速入門中,您將使用 .NET SDK 共用資料,並從 Azure Data Lake Storage (ADLS Gen2) 或 Blob 儲存體帳戶接收共用。 本文包含可讓您使用 Microsoft Purview 資料共用建立、接受及管理共用的程式碼片段。

如需資料共用運作方式的概觀,請觀看這個簡短 的示範。

注意事項

此功能已於 2023 年 2 月更新,且已變更檢視和管理 Microsoft Purview 中資料共用所需的 SDK 和權限。

  • Microsoft Purview 現在不需要任何權限即可使用 SDK 建立和管理共用。 (需要讀取者權限才能使用 Microsoft Purview 管理入口網站共用資料。)
  • 儲存體帳戶仍需要權限。

請參閱更新的 NuGet 套件 和 更新的程式碼片段 ,以使用更新的 SDK。

必要條件

Microsoft Purview 必要條件

  • Microsoft Purview 帳戶。 您也可以使用兩個 Microsoft Purview 帳戶,一個用於資料提供者,一個用於資料取用者,以測試這兩個案例。
  • 收件者的 Azure 登入電子郵件地址,您可以用來傳送邀請。 收件者的電子郵件別名無法運作。

Azure 儲存帳戶必要條件

  • 您的 Azure 訂用帳戶必須註冊 AllowDataSharing 預覽功能。 (如果您尚未註冊此預覽功能,請考慮改為使用 Microsoft Fabric 外部資料共用 ,因為 Microsoft Purview 資料共用將於 2025 年 9 月淘汰。) 或者, 請連絡支援人員。
  • 註冊步驟完成後 所建立 的來源和目標儲存體帳戶。 兩個儲存體帳戶都必須彼此位於相同的 Azure 區域中。 兩個儲存體帳戶都必須是 ADLS Gen2 或 Blob 儲存體帳戶。 您的儲存體帳戶可能與 Microsoft Purview 帳戶位於不同的 Azure 區域。
  • 最新版本的儲存體 SDK、PowerShell、CLI 和 Azure 儲存體總管。 儲存體 REST API 版本必須為 2020 年 2 月或更新版本。
  • 儲存體帳戶必須在您將傳送或接收共用的集合中註冊。 如果您使用一個 Microsoft Purview 帳戶,這可能是兩個不同的集合,或相同的集合。 如需註冊指示,請參閱 ADLS Gen2 或 Blob 儲存體 資料來源頁面。
  • 如果來源或目標儲存體帳戶位於與 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. 在 建立 Microsoft Entra 應用程式中,建立代表您在本教學課程中建立的 .NET 應用程式的應用程式。 針對登入 URL,可以提供虛擬 URL,如 (https://contoso.org/exampleapp) 一文所示。

  2. 在 [取得登入的值] 中,取得 應用程式識別碼、租用戶識別碼和 物件識別碼,並記下您稍後在本教學課程中使用的這些值。

  3. 在 [憑證和密碼] 中,取得 驗證金鑰,並記下本教學課程稍後會使用的此值。

  4. 將應用程式指派給下列角色:

    使用者 Azure 儲存帳戶角色 Microsoft Purview 集合角色
    資料提供者 下列其中一個角色:
    • 擁有者
    • Blob 儲存體資料擁有者
    資料共用參與者
    資料消費者 下列其中一個角色:
    • 投稿者
    • 擁有者
    • 儲存體 Blob 資料參與者
    • Blob 儲存體資料擁有者
    資料共用參與者

建立 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 <name of the project folder>' 向下移動一個級別到您的專案資料夾。 然後再試一次。

建立已傳送的共用

此指令碼會建立資料共用,您可以傳送給內部或外部使用者。 若要使用它,請務必填寫這些變數:

  • SenderTenantId - 寄件者身分識別的 Azure 租用戶識別 碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • ShareName - 已傳送共用的顯示名稱。
  • ShareDescription - (選用) 已傳送共用的描述。
  • SenderStorageKind - BlobAccount 或 AdlsGen2Account。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • SenderStorageContainer - 儲存要共用資料的容器名稱。
  • SenderPathToShare - 要共用的資料的檔案/資料夾路徑。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選用) 如果使用服務主體建立共用,請新增您的用戶端密碼/驗證金鑰。
  • SentShareID - (選用) 此選項必須是 GUID,目前值會為您產生一個 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 - 使用者要傳送邀請的電子郵件地址。
  • SenderTenantId - 共用寄件者身分識別的 Azure 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • SentShareDisplayName - 您要為其傳送邀請的已傳送共用名稱。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選用) 如果使用服務主體建立共用,請新增您的用戶端密碼/驗證金鑰。
  • InvitationId - (選用) 此選項必須是 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;
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 租用戶識別碼。
  • RecipientApplicationObjectId - 接收服務主體的物件識別碼。
  • SenderTenantId - 共用寄件者身分識別的 Azure 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • SentShareDisplayName - 您要為其傳送邀請的已傳送共用名稱。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選用) 如果使用服務主體建立共用,請新增您的用戶端密碼/驗證金鑰。
  • InvitationId - (選用) 此選項必須是 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;
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 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SenderStorageResourceId - 儲存 體帳戶的資源識別碼 ,其共用的傳送來源。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SentShareDisplayName - 您列出收件者的已傳送共用名稱。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SentShareDisplayName - 要移除收件者的已傳送共用的名稱。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • RecipientUserEmailId - 您要刪除之使用者的電子郵件地址。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • SenderPurviewAccountName - 資料的來源 Microsoft Purview 帳戶名稱。
  • SentShareDisplayName - 您列出收件者的已傳送共用名稱。
  • SenderStorageResourceId - 資料的來源儲存 體帳戶的資源識別碼 。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體來建立共用,請將此設定為 True。
  • SenderClientId - (選用) 如果使用服務主體建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • ReceiverPurviewAccountName - 將接收資料的 Microsoft Purview 帳戶名稱。
  • ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
  • ReceiverStorageResourceId - 將接收資料的 儲存體帳戶的資源識別碼 。
  • ReceiverStorageContainer - 將儲存共用資料的容器名稱。
  • ReceiverTargetFolderName - 將儲存共用資料的資料夾路徑。
  • ReceiverTargetMountPath - 您要用來在資料夾中儲存資料的掛接路徑。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體接收共用,請將此設定為 True。
  • ReceiverClientId - (選用) 如果使用服務主體接收共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • ReceiverClientSecret - (選用) 如果使用服務主體接收共用,請新增您的用戶端密碼/驗證金鑰。
  • ReceivedShareId - (選用) 此選項必須是 GUID,而目前值會為您產生一個 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 租用戶識別碼。
  • ReceiverPurviewAccountName - 收到資料的 Microsoft Purview 帳戶名稱。
  • ReceiverStorageResourceId - 共用資料之 儲存體帳戶的資源識別碼 。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體接收共用,請將此設定為 True。
  • ReceiverClientId - (選用) 如果使用服務主體接收共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • ReceiverPurviewAccountName - 將接收資料的 Microsoft Purview 帳戶名稱。
  • ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
  • ReceiverStorageResourceId - 共用資料之 儲存體帳戶的資源識別碼 。
  • ReAttachStorageResourceId - 將接收資料的 儲存體帳戶的資源識別碼 。
  • ReceiverStorageContainer - 將儲存共用資料的容器名稱。
  • ReceiverTargetFolderName - 將儲存共用資料的資料夾路徑。
  • ReceiverTargetMountPath - 您要用來在資料夾中儲存資料的掛接路徑。
  • ReceivedShareDisplayName - 收到之共用的顯示名稱。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體接收共用,請將此設定為 True。
  • ReceiverClientId - (選用) 如果使用服務主體接收共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 租用戶識別碼。
  • ReceiverPurviewAccountName - 將接收資料的 Microsoft Purview 帳戶名稱。
  • ReceivedShareDisplayName - 收到之共用的顯示名稱。
  • ReceiverStorageResourceId - 共用資料之 儲存體帳戶的資源識別碼 。
  • UseServiceTokenCredentials - (選用) 如果您想要使用服務主體接收共用,請將此設定為 True。
  • ReceiverClientId - (選用) 如果使用服務主體接收共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • 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 在目標儲存體帳戶中建立的目標容器和資料夾。

後續步驟