快速入門:使用 Microsoft Purview 資料共用 .NET SDK 共用和接收數據
注意事項
Microsoft Purview 資料目錄 (傳統) 和數據健康情況深入解析 (傳統) 不再採用新客戶,而這些服務先前是 Azure Purview,現在處於客戶支援模式。
重要事項
這項功能目前正在淘汰,並提供到 2025 年 9 月的支援,以允許移轉至 Microsoft 網狀架構外部數據共用。
在本快速入門中,您將使用 .NET SDK 來共享數據,並從 #D85A96A5BC79748D4934762577C4E90AB (ADLS Gen2) 或 Blob 記憶體帳戶接收共用。 本文包含代碼段,可讓您使用 Microsoft Purview 資料共用 建立、接受及管理共用。
如需數據共享運作方式的概觀,請 watch 此簡短示範。
注意事項
這項功能已在 2023 年 2 月更新,且在 Purview 中檢視和管理數據共用所需的 SDK 和許可權Microsoft已變更。
- Microsoft Purview 現在不需要任何許可權,即可使用 SDK 來建立和管理共用。 需要 (讀取者許可權,才能使用 Microsoft Purview 治理入口網站來共享數據。)
- 記憶體帳戶仍需要許可權。
必要條件
Microsoft Purview 必要條件
- Microsoft Purview 帳戶。 您也可以使用兩個 Microsoft Purview 帳戶,一個用於數據提供者,另一個供數據取用者測試這兩個案例。
- 您收件者的 Azure 登入電子郵件位址,可用來傳送邀請。 收件者的電子郵件別名無法運作。
Azure 記憶體帳戶必要條件
- 您的 Azure 訂用帳戶必須註冊 AllowDataSharing 預覽功能。 (如果您尚未註冊此預覽功能,請考慮改用 Microsoft Fabric 外部數據共享,因為 Microsoft Purview 資料共用 將於 2025 年 9 月淘汰。) 或連絡支持人員。
- 註冊步驟完成 後所建立 的來源和目標記憶體帳戶。 這兩個記憶體帳戶必須彼此位於相同的 Azure 區域。 這兩個記憶體帳戶都必須是ADLS Gen2或 Blob 記憶體帳戶。 您的記憶體帳戶可以位於與 Microsoft Purview 帳戶不同的 Azure 區域中。
- 最新版的記憶體 SDK、PowerShell、CLI 和 Azure 儲存體總管。 記憶體 REST API 版本必須是 2020 年 2 月或更新版本。
- 記憶體帳戶必須在您要傳送或接收共用的集合中註冊。 如果您使用一個 Microsoft Purview 帳戶,這可以是兩個不同的集合,或是相同的集合。 如需註冊的指示,請參閱 ADLS Gen2 或 Blob 記憶體 數據源頁面。
- 如果來源或目標記憶體帳戶位於與 Microsoft Purview 帳戶不同的 Azure 訂用帳戶中,則會Microsoft。 當共用提供者新增資產或共用取用者對應資產時,Purview 資源提供者會自動在數據存放區所在的 Azure 訂用帳戶中註冊, 只有在 使用者有權執行資源提供者的 /register/action 作業時才註冊。 許可權包含在參與者和擁有者角色中。
注意事項
只有在將數據共用或接收到 Azure 訂用帳戶中的記憶體帳戶時,才需要第一次註冊。
所需角色
以下是共享數據和接收共用的必要角色。
Azure 記憶體帳戶角色 | Microsoft Purview 集合角色 | |
---|---|---|
數據提供者 | 下列其中一個角色:
|
數據讀取器 |
數據取用者 | 下列其中一個角色:
|
數據讀取器 |
注意事項
如果您已建立 Microsoft Purview 帳戶,則會自動將所有角色指派給根集合。 請參閱 Microsoft Purview 許可權 ,以深入瞭解 Microsoft Purview 集合和角色。
Visual Studio
本文中的逐步解說使用Visual Studio 2022。 Visual Studio 2013、2015、2017 或 2019 的程式可能稍有不同。
Azure .NET SDK
在您的電腦上下載並安裝 Azure .NET SDK 。
使用服務主體
在本教學課程的代碼段中,您可以使用自己的認證或使用服務主體進行驗證。 若要設定服務主體,請遵循下列指示:
在 [建立 Microsoft Entra 應用程式] 中,建立代表您在本教學課程中建立之 .NET 應用程式的應用程式。 針對登入 URL,您可以提供虛擬 URL,如 ()
https://contoso.org/exampleapp
一文所示。在 [取得登入的值] 中,取得 應用程式標識碼、租使用者標識碼和 對象標識碼,並記下您稍後在本教學課程中使用的這些值。
在 [憑證和秘密] 中,取得 驗證密鑰,並記下您稍後在本教學課程中使用的此值。
-
使用者 Azure 記憶體帳戶角色 Microsoft Purview 集合角色 數據提供者 下列其中一個角色: - 擁有者
- Blob 記憶體數據擁有者
Data Share 參與者 數據取用者 下列其中一個角色: - 投稿者
- 擁有者
- 記憶體 Blob 數據參與者
- Blob 記憶體數據擁有者
Data Share 參與者
建立 Visual Studio 專案
接下來,在 Visual Studio 中建立 C# .NET 控制台應用程式:
- 啟動 Visual Studio。
- 在 [開始] 視窗中,選取 [建立新的專案>控制台應用程式]。 需要 .NET 6.0 版或更新版本。
- 在 [項目名稱] 中,輸入 PurviewDataSharingQuickStart。
- 選 取 [建立 ] 以建立專案。
安裝 NuGet 套件
選 取 [工具>][NuGet 套件管理員>套件管理員控制台]。
在 [ 套件管理控制台 ] 中,執行此頁面上顯示的 .NET cli add package 命令,以新增 NuGet 套件: Microsoft.Azure.Analytics.Purview.Sharing NuGet 套件。
在 [ 套件管理員控制台] 窗格中,執行下列命令以安裝套件。
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檢視體驗,請使用
$"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檢視體驗,請使用
$"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檢視體驗,請使用
$"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檢視體驗,請使用
$"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;
}
}
清除資源
若要清除為快速入門建立的資源,請使用下列指導方針:
- 在 Microsoft Purview 入口網站中, 刪除已傳送的共用。
- 也 請刪除您收到的共用。
- 成功刪除共享之後,請在收到共享數據時,刪除目標記憶體帳戶中建立Microsoft Purview 的目標容器和資料夾。