共用方式為


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

注意事項

Microsoft Purview 資料目錄 (傳統) 和 Data Health Insights (傳統) 不再接受新客戶,而且這些服務 (先前為 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 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. 建立 Microsoft Entra 應用程式中,建立代表您在本教學課程中建立的 .NET 應用程式的應用程式。 對於登入 URL,您可以提供虛擬 URL,如) (https://contoso.org/exampleapp 一文所示。

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

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

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

    使用者 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 租用戶識別碼
  • SenderPurviewAccountName - 將從中傳送資料的 Microsoft Purview 帳戶名稱。
  • 共用名稱 - 已傳送共用的顯示名稱。
  • ShareDescription - (選擇性) 已傳送共用的描述。
  • SenderStorageKind - BlobAccount 或 AdlsGen2Account。
  • SenderStorageResourceId - 將從中傳送資料之 儲存體帳戶的資源識別碼
  • SenderStorageContainer - 儲存要共用資料的容器名稱。
  • SenderPathToShare - 要共用之資料的檔案/資料夾路徑。
  • UseServiceTokenCredentials - (選擇性) 如果您想要使用服務主體來建立共用,請將此設定為 true
  • SenderClientId - (選擇性) 如果使用服務主體來建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選擇性) 如果使用服務主體來建立共用,請新增用戶端密碼/驗證金鑰。
  • SentShareID - (選擇性) 此選項必須是 GUID,而目前的值會為您產生 GUID,但您可以視需要將它取代為不同的值。
  • ReceiverVisiblePath - (選擇性) 接收者將看到的共用名稱。 目前設定為 GUID,但不需要 GUID。
  • SenderPurviewEndPoint - 如果您使用 如果您使用傳統 Microsoft 權限體驗,請使用 $"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 租用戶識別碼。
  • SenderPurviewAccountName - 將從中傳送資料的 Microsoft Purview 帳戶名稱。
  • SenderStorageResourceId - 將從中傳送資料之 儲存體帳戶的資源識別碼
  • SentShareDisplayName - 您要傳送邀請的已傳送共用名稱。
  • UseServiceTokenCredentials - (選擇性) 如果您想要使用服務主體來建立共用,請將此設定為 true
  • SenderClientId - (選擇性) 如果使用服務主體來建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選擇性) 如果使用服務主體來建立共用,請新增用戶端密碼/驗證金鑰。
  • InvitationId - (選擇性) 此選項必須是 GUID,目前的值會為您產生 GUID,但您可以視需要將其取代為不同的值。
  • SenderPurviewEndPoint - 如果您使用 如果您使用傳統 Microsoft 權限體驗,請使用 $"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 權限體驗,請使用 $"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 權限體驗,請使用 $"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 權限體驗,請使用 $"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 - 您要刪除之使用者的 Email 位址。
  • UseServiceTokenCredentials - (選擇性) 如果您想要使用服務主體來建立共用,請將此設定為 true
  • SenderClientId - (選擇性) 如果使用服務主體來建立共用,這是服務主體的應用程式 (用戶端) 識別碼。
  • SenderClientSecret - (選擇性) 如果使用服務主體來建立共用,請新增用戶端密碼/驗證金鑰。
  • SenderPurviewEndPoint - 如果您使用 如果您使用傳統 Microsoft 權限體驗,請使用 $"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 權限體驗,請使用 $"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 在目標儲存體帳戶中建立的目標容器和資料夾。

後續步驟