共用方式為


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

重要事項

此功能目前處於預覽。 Microsoft Azure 預覽版的補充使用規定包含適用于 Beta、預覽版或尚未正式上市之 Azure 功能的其他法律條款。

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

如需資料共用運作方式的概觀,請watch此簡短示範

注意事項

這項功能已在 2023 年 2 月更新,且在 Microsoft Purview 中檢視和管理資料共用所需的 SDK 和許可權已變更。

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

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

必要條件

Microsoft Purview 必要條件

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

Azure 儲存體帳戶必要條件

  • 您的 Azure 訂用帳戶必須註冊 AllowDataSharing 預覽功能。 使用 Azure 入口網站 或 PowerShell 遵循下列步驟。

    1. 在Azure 入口網站中,選取您將用來建立來源和目標儲存體帳戶的 Azure 訂用帳戶。
    2. 從左側功能表中,選取 [設定] 底下的[預覽功能]。
    3. 取 [AllowDataSharing ] 和 [ 註冊]
    4. 重新 整理 [預覽功能] 畫面,以確認 [狀態 ] 為 [ 已註冊]。 註冊可能需要 15 分鐘到 1 小時才能完成。
    5. 此外,若要將資料共用用於美國東部、美國東部 2、北歐、美國中南部、美國中西部、西歐、美國西部、美國西部 2、美國西部 3 的儲存體帳戶:選取 [AllowDataSharingInHeroRegion] 和 [註冊]

    如需詳細資訊,請 參閱註冊預覽功能

    RegistrationState應該是 Registered。 註冊可能需要 15 分鐘到 1 小時才能完成。 如需詳細資訊,請 參閱註冊預覽功能

    注意事項

    以下是支援的儲存體帳戶設定:

    • Azure 區域:加拿大中部、加拿大東部、英國南部、英國西部、澳大利亞東部、日本東部、南韓南部和南非北部
    • 其他 Azure 區域:美國東部、美國東部 2、北歐、美國中南部、美國中西部、西歐、美國西部、美國西部 2、美國西部3
    • 效能:標準
    • 備援選項:LRS
  • 註冊步驟完成 後所建立 的來源和目標儲存體帳戶。 這兩個儲存體帳戶必須彼此位於相同的 Azure 區域。 這兩個儲存體帳戶都必須是 ADLS Gen2 或 Blob 儲存體帳戶。 您的儲存體帳戶可以位於與 Microsoft Purview 帳戶不同的 Azure 區域中。

  • 最新版的儲存體 SDK、PowerShell、CLI 和 Azure 儲存體總管。 儲存體 REST API 版本必須是 2020 年 2 月或更新版本。

  • 儲存體帳戶必須在您要傳送或接收共用的集合中註冊。 如果您使用一個 Microsoft Purview 帳戶,這可以是兩個不同的集合,或是相同的集合。 如需註冊的指示,請參閱 ADLS Gen2Blob 儲存體 資料來源頁面。

  • 如果來源或目標儲存體帳戶位於與 Microsoft Purview 帳戶不同的 Azure 訂用帳戶中,則 Microsoft.Purview 資源提供者會自動在資料存放區位於共用提供者新增資產或共用取用者對應資產時所在的 Azure 訂用帳戶中註冊, 而且只有在 使用者有權為資源提供者執行 /register/action 作業時才會註冊。 許可權包含在參與者和擁有者角色中。

    注意事項

    只有在將資料共用或接收到 Azure 訂用帳戶中的儲存體帳戶時,才需要第一次註冊。

所需角色

以下是共用資料和接收共用的必要角色。

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

注意事項

如果您建立了 Microsoft Purview 帳戶,系統會自動將所有角色指派給根集合。 若要深入瞭解 Microsoft Purview 集合和角色,請參閱 Microsoft Purview 許可權。

Visual Studio

本文中的逐步解說使用 Visual Studio 2022。 Visual Studio 2013、2015、2017 或 2019 的程式可能稍有不同。

Azure .NET SDK

在您的電腦上下載並安裝 Azure .NET SDK

使用服務主體

在本教學課程的程式碼片段中,您可以使用自己的認證或使用服務主體進行驗證。 若要設定服務主體,請遵循下列指示:

  1. [建立 Azure Active Directory應用程式] 中,建立代表您在本教學課程中建立之 .NET 應用程式的應用程式。 針對登入 URL,您可以提供虛擬 URL,如 () https://contoso.org/exampleapp 一文所示。

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

  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 新增套件命令,以新增 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 - (所傳送共用的選擇性) A 描述。
  • 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 - Email使用者傳送邀請的位址。
  • 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 - Email您要刪除之使用者的位址。
  • 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。

後續步驟