重要
仅停用 Microsoft Purview 数据治理功能
Microsoft Purview 统一目录和经典 Microsoft Purview 数据治理功能 (以前称为 Azure Purview) 即将停用。 Microsoft Purview 整体未停用。 Microsoft Purview 数据安全性和合规性功能不受此公告的影响,Microsoft 将继续对其进行投资。
Microsoft 正在 Microsoft Fabric 中整合其 数据治理 功能。 无法再创建新的 Microsoft Purview 数据治理帐户,现有数据治理功能将在 2027 年 11 月 12 日前保持维护模式。
有关停用时间表、过渡指南和建议的后续步骤,请参阅 Microsoft Purview 数据治理停用公告。
注意
Microsoft Purview 数据目录 (经典) 、Data Health Insights (经典) 和 Purview 工作流 (经典) 不再接受新客户,这些服务(以前为 Azure Purview)现在处于客户支持模式。
重要
此功能当前已停用,支持将持续到 2025 年 9 月,以便迁移到 Microsoft Fabric 外部数据共享。
在本快速入门中,你将使用 .NET SDK 共享数据并从 Azure Data Lake Storage (ADLS Gen2) 或 Blob 存储帐户接收共享。 本文包含一些代码片段,可用于使用 Microsoft Purview 数据共享创建、接受和管理共享。
如需了解数据共享的工作原理,请观看此简短 演示。
注意
此功能已于 2023 年 2 月更新,并且在 Microsoft Purview 中查看和管理数据共享所需的 SDK 和权限已更改。
- 现在,无需在 Microsoft Purview 中拥有任何权限即可使用 SDK 创建和管理共享。 (需要读取者权限才能使用 Microsoft Purview 治理门户共享数据。)
- 存储帐户仍需要权限。
先决条件
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。 在共享提供程序添加资产或共享使用者映射资产时,并且仅当用户有权对资源提供程序执行 /register/action 操作 时,才 会在数据存储所在的 Azure 订阅中注册 Purview 资源提供程序自动注册。 权限包含在参与者和所有者角色中。
注意
只有在第一次将数据共享或接收到 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) 一文中所示。在“ 获取用于登录的值”中,获取 应用程序 ID、租户 ID 和 对象 ID,并记下稍后在本教程中使用的这些值。
在 “证书和机密”中,获取 身份验证密钥,并记下本教程稍后将使用的此值。
-
用户 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 租户 ID 。
- SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
- ShareName - 已发送共享的显示名称。
- ShareDescription - (optional) 已发送共享的说明。
- SenderStorageKind - BlobAccount 或 AdlsGen2Account。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- SenderStorageContainer - 存储要共享的数据的容器的名称。
- SenderPathToShare - 要共享的数据的文件/文件夹路径。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端密码/身份验证密钥。
- SentShareID - (可选) 此选项必须是 GUID,当前值将为你生成一个 GUID,但如果需要,可以将其替换为其他值。
- ReceiverVisiblePath - (可选) 接收方将看到的共享名称。 当前设置为 GUID,但不需要 GUID。
-
SenderPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{SenderPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string SenderTenantId = "<Sender Identity's Tenant ID>";
private static string SenderPurviewAccountName = "<Sender Purview Account Name>";
private static string ShareName = "<Share Display Name>";
private static string ShareDescription = "Share created using the SDK.";
private static string SenderStorageKind = "<Sender Storage Account Kind (BlobAccount / AdlsGen2Account)>";
private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
private static string SenderStorageContainer = "<Share Data Container Name>";
private static string SenderPathToShare = "<File/Folder Path To Share>";
// Set if using Service principal to create shares
private static bool UseServiceTokenCredentials = false;
private static string SenderClientId = "<Sender Application (Client) Id>";
private static string SenderClientSecret = "<Sender Application (Client) Secret>";
// [OPTIONAL INPUTS] Override Value If Desired.
private static string SentShareId = Guid.NewGuid().ToString();
private static string ReceiverVisiblePath = Guid.NewGuid().ToString();
// General Configs
private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
/// Replace all placeholder inputs above with actual values before running this program.
/// This updated Share experience API will create Shares based on callers RBAC role on the storage account.
/// To view/manage Shares via UX in Purview Studio. Storage accounts need to be registered (one time action) in Purview account with DSA permissions.
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateShare - START");
await Sender_CreateSentShare();
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateShare - FINISH");
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
private static async Task<BinaryData> Sender_CreateSentShare()
{
TokenCredential senderCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });
SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);
if (sentSharesClient == null)
{
throw new InvalidEnumArgumentException("Invalid Sent Shares Client.");
}
// Create sent share
var inPlaceSentShareDto = new
{
shareKind = "InPlace",
properties = new
{
displayName = ShareName,
description = ShareDescription,
artifact = new
{
storeKind = SenderStorageKind,
storeReference = new
{
referenceName = SenderStorageResourceId,
type = "ArmResourceReference"
},
properties = new
{
paths = new[]
{
new
{
receiverPath = ReceiverVisiblePath,
containerName = SenderStorageContainer,
senderPath = SenderPathToShare
}
}
}
}
},
};
Operation<BinaryData> sentShare = await sentSharesClient.CreateOrReplaceSentShareAsync(WaitUntil.Completed, SentShareId, RequestContent.Create(inPlaceSentShareDto));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(sentShare.Value);
Console.ForegroundColor = Console.ForegroundColor;
return sentShare.Value;
}
}
向用户发送邀请
此脚本向用户发送共享电子邮件邀请。 如果要向服务主体发送邀请, 请参阅以下代码示例。 若要使用它,请务必填写以下变量:
- RecipientUserEmailId - 要向其发送邀请的用户电子邮件地址。
- SenderTenantId - 共享发件人标识的 Azure 租户 ID。
- SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- SentShareDisplayName - 要为其发送邀请的已发送共享的名称。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- 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 租户 ID。
- RecipientApplicationObjectId - 接收服务主体的对象 ID。
- SenderTenantId - 共享发件人标识的 Azure 租户 ID。
- SenderPurviewAccountName - 将从中发送数据的 Microsoft Purview 帐户的名称。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- SentShareDisplayName - 要为其发送邀请的已发送共享的名称。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- 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 租户 ID。
- SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
- SenderStorageResourceId - 从中发送共享的 存储帐户的资源 ID 。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端密码/身份验证密钥。
-
SenderPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{SenderPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string SenderTenantId = "<Sender Tenant ID>";
private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
// Set if using Service principal to list shares
private static bool UseServiceTokenCredentials = false;
private static string SenderClientId = "<Sender Application (Client) Id>";
private static string SenderClientSecret = "<Sender Application (Client) Secret>";
// General Configs
private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential senderCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });
SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);
var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
Console.WriteLine(allSentShares);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
列出所有共享收件人
此脚本列出特定共享的所有收件人。 若要使用它,请务必填写以下变量:
- SenderTenantId - 共享发件人标识的 Azure 租户 ID。
- SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
- SentShareDisplayName - 为其列出收件人的已发送共享的名称。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端密码/身份验证密钥。
-
SenderPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{SenderPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string SentShareDisplayName = "<Name of share you're listing recipients for.>";
private static string SenderTenantId = "<Sender Tenant ID>";
private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
// Set if using Service principal to list recipients
private static bool UseServiceTokenCredentials = false;
private static string SenderClientId = "<Sender Application (Client) Id>";
private static string SenderClientSecret = "<Sender Application (Client) Secret>";
// General Configs
private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential senderCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });
SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);
var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
var mySentShare = allSentShares.First(sentShareDoc =>
{
var doc = JsonDocument.Parse(sentShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == SentShareDisplayName;
});
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("My Sent Share Id: " + JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString());
Console.ForegroundColor = Console.ForegroundColor;
var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();
var allRecipients = await sentSharesClient.GetAllSentShareInvitationsAsync(SentShareId).ToResultList();
Console.WriteLine(allRecipients);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
删除收件人
此脚本将删除收件人的共享邀请,从而删除共享。 若要使用它,请务必填写以下变量:
- SenderTenantId - 共享发件人标识的 Azure 租户 ID。
- SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
- SentShareDisplayName - 要删除其收件人的已发送共享的名称。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- RecipientUserEmailId - 要删除的用户的电子邮件地址。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端密码/身份验证密钥。
-
SenderPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{SenderPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string SentShareDisplayName = "<Name of share you're removing a recipient for.>";
private static string SenderTenantId = "<Sender Tenant ID>";
private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
private static string RecipientUserEmailId = "<Target User's Email Id>";
private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
// Set if using Service principal to delete recipients
private static bool UseServiceTokenCredentials = false;
private static string SenderClientId = "<Sender Application (Client) Id>";
private static string SenderClientSecret = "<Sender Application (Client) Secret>";
// General Configs
private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential senderCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });
SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);
var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
var mySentShare = allSentShares.First(sentShareDoc =>
{
var doc = JsonDocument.Parse(sentShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == SentShareDisplayName;
});
var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();
var allRecipients = await sentSharesClient.GetAllSentShareInvitationsAsync(SentShareId).ToResultList();
var recipient = allRecipients.First(recipient =>
{
var doc = JsonDocument.Parse(recipient).RootElement;
var props = doc.GetProperty("properties");
return props.TryGetProperty("targetEmail", out JsonElement rcpt) && rcpt.ToString() == RecipientUserEmailId;
});
var recipientId = JsonDocument.Parse(recipient).RootElement.GetProperty("id").ToString();
await sentSharesClient.DeleteSentShareInvitationAsync(WaitUntil.Completed, SentShareId, recipientId);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Remove Id: " + JsonDocument.Parse(recipient).RootElement.GetProperty("id").ToString());
Console.WriteLine("Complete");
Console.ForegroundColor = Console.ForegroundColor;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
删除已发送的共享
此脚本将删除已发送的共享。 若要使用它,请务必填写以下变量:
- SenderTenantId - 共享发件人标识的 Azure 租户 ID。
- SenderPurviewAccountName - 从中发送数据的 Microsoft Purview 帐户的名称。
- SentShareDisplayName - 为其列出收件人的已发送共享的名称。
- SenderStorageResourceId - 将从中发送数据的 存储帐户的资源 ID 。
- UseServiceTokenCredentials - (可选) 如果要使用服务主体创建共享,请将此项设置为 true。
- SenderClientId - (可选) 如果使用服务主体创建共享,则这是服务主体的应用程序 (客户端) ID。
- SenderClientSecret - (可选) 如果使用服务主体创建共享,请添加客户端密码/身份验证密钥。
-
SenderPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{SenderPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string SenderTenantId = "<Sender Tenant ID>";
private static string SenderPurviewAccountName = "<Name of the Microsoft Purview account>";
private static string SentShareDisplayName = "<Name of share you're removing.>";
private static string SenderStorageResourceId = "<Sender Storage Account Resource Id>";
// Set if using Service principal to delete share
private static bool UseServiceTokenCredentials = false;
private static string SenderClientId = "<Sender Application (Client) Id>";
private static string SenderClientSecret = "<Sender Application (Client) Secret>";
// General Configs
private static string SenderPurviewEndPoint = $"https://{SenderPurviewAccountName}.purview.azure.com";
private static string SenderShareEndPoint = $"{SenderPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential senderCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(SenderTenantId, SenderClientId, SenderClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = SenderTenantId });
SentSharesClient? sentSharesClient = new SentSharesClient(SenderShareEndPoint, senderCredentials);
var allSentShares = await sentSharesClient.GetAllSentSharesAsync(SenderStorageResourceId).ToResultList();
var mySentShare = allSentShares.First(sentShareDoc =>
{
var doc = JsonDocument.Parse(sentShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == SentShareDisplayName;
});
var SentShareId = JsonDocument.Parse(mySentShare).RootElement.GetProperty("id").ToString();
await sentSharesClient.DeleteSentShareAsync(WaitUntil.Completed, SentShareId);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Remove Id: " + SentShareId);
Console.WriteLine("Complete");
Console.ForegroundColor = Console.ForegroundColor;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
创建已接收共享
此脚本允许接收数据共享。 若要使用它,请务必填写以下变量:
- ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
- ReceiverPurviewAccountName - 将在其中接收数据的 Microsoft Purview 帐户的名称。
- ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
- ReceiverStorageResourceId - 将在其中接收数据 的存储帐户的资源 ID 。
- ReceiverStorageContainer - 存储共享数据的容器的名称。
- ReceiverTargetFolderName - 存储共享数据的文件夹路径。
- ReceiverTargetMountPath - 用于在文件夹中存储数据的装载路径。
- UseServiceTokenCredentials - (可选) 如果你想要使用服务主体接收共享,请将此项设置为 true。
- ReceiverClientId - (可选) 如果使用服务主体接收共享,则这是服务主体的应用程序 (客户端) ID。
- ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端密码/身份验证密钥。
- ReceivedShareId - (可选) 此选项必须是 GUID,当前值将为你生成一个 GUID,但如果需要,你可以将其替换为其他值。
- ReceiverVisiblePath - (要用于已接收共享路径的可选) 名称。
- ReceivedShareDisplayName - (可选) 已接收共享的显示名称。
-
ReceiverPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{ReceiverPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";
private static string ReceiverStorageKind = "<Receiver Storage Account Kind (BlobAccount / AdlsGen2Account)>";
private static string ReceiverStorageResourceId = "<Receiver Storage Account Resource Id>";
private static string ReceiverStorageContainer = "<Container Name To Receive Data Under>";
private static string ReceiverTargetFolderName = "<Folder Name to Received Data Under>";
private static string ReceiverTargetMountPath = "<Mount Path to store Received Data Under>";
//Use if using a service principal to receive a share
private static bool UseServiceTokenCredentials = false;
private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";
// [OPTIONAL INPUTS] Override Values If Desired.
private static string ReceivedShareId = Guid.NewGuid().ToString();
private static string ReceiverVisiblePath = "ReceivedSharePath";
private static string ReceivedShareDisplayName = "ReceivedShare";
// General Configs
private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateReceivedShare - START");
await Receiver_CreateReceivedShare();
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: CreateReceivedShare - FINISH");
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
private static async Task<BinaryData> Receiver_CreateReceivedShare()
{
TokenCredential receiverCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });
ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);
if (receivedSharesClient == null)
{
throw new InvalidEnumArgumentException("Invalid Received Shares Client.");
}
var results = await receivedSharesClient.GetAllDetachedReceivedSharesAsync().ToResultList();
var detachedReceivedShare = results;
if (detachedReceivedShare == null)
{
throw new InvalidOperationException("No received shares found.");
}
var myReceivedShare = detachedReceivedShare.First(recShareDoc =>
{
var doc = JsonDocument.Parse(recShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
});
var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();
var attachedReceivedShareData = new
{
shareKind = "InPlace",
properties = new
{
displayName = ReceivedShareDisplayName,
sink = new
{
storeKind = ReceiverStorageKind,
properties = new
{
containerName = ReceiverStorageContainer,
folder = ReceiverTargetFolderName,
mountPath = ReceiverTargetMountPath
},
storeReference = new
{
referenceName = ReceiverStorageResourceId,
type = "ArmResourceReference"
}
}
}
};
var receivedShare = await receivedSharesClient.CreateOrReplaceReceivedShareAsync(WaitUntil.Completed, ReceivedShareId, RequestContent.Create(attachedReceivedShareData));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(receivedShare.Value);
Console.ForegroundColor = Console.ForegroundColor;
return receivedShare.Value;
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
列出所有已接收的共享
此脚本列出存储帐户上所有已接收的共享。 若要使用它,请务必填写以下变量:
- ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
- ReceiverPurviewAccountName - 接收数据的 Microsoft Purview 帐户的名称。
- ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID 。
- UseServiceTokenCredentials - (可选) 如果你想要使用服务主体接收共享,请将此项设置为 true。
- ReceiverClientId - (可选) 如果使用服务主体接收共享,则这是服务主体的应用程序 (客户端) ID。
- ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端密码/身份验证密钥。
-
ReceiverPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{ReceiverPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";
private static string ReceiverStorageResourceId = "<Storage Account Resource Id that is housing shares>";
//Use if using a service principal to list shares
private static bool UseServiceTokenCredentials = false;
private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";
// General Configs
private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential receiverCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });
ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);
var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();
Console.WriteLine(allReceivedShares);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
更新接收的共享
此脚本允许您更新已接收共享的存储位置。 就像创建已接收共享一样,为要存放数据的存储帐户添加信息。 若要使用它,请务必填写以下变量:
- ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
- ReceiverPurviewAccountName - 将在其中接收数据的 Microsoft Purview 帐户的名称。
- ReceiverStorageKind - BlobAccount 或 AdlsGen2Account。
- ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID 。
- ReAttachStorageResourceId - 将接收数据 的存储帐户的资源 ID 。
- ReceiverStorageContainer - 存储共享数据的容器的名称。
- ReceiverTargetFolderName - 存储共享数据的文件夹路径。
- ReceiverTargetMountPath - 用于在文件夹中存储数据的装载路径。
- ReceivedShareDisplayName - 已接收共享的显示名称。
- UseServiceTokenCredentials - (可选) 如果你想要使用服务主体接收共享,请将此项设置为 true。
- ReceiverClientId - (可选) 如果使用服务主体接收共享,则这是服务主体的应用程序 (客户端) ID。
- ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端密码/身份验证密钥。
-
ReceiverPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{ReceiverPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.ComponentModel;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";
private static string ReceiverStorageKind = "<Receiver Storage Account Kind (BlobAccount / AdlsGen2Account)>";
private static string ReceiverStorageResourceId = "<Storage Account Resource Id for the account where the share is currently attached.>";
private static string ReAttachStorageResourceId = "<Storage Account Resource Id For Reattaching Received Share>";
private static string ReceiverStorageContainer = "<Container Name To Receive Data Under>";
private static string ReceiverTargetFolderName = "<Folder Name to Received Data Under>";
private static string ReceiverTargetMountPath = "<Mount Path to Received Data Under>";
private static string ReceivedShareDisplayName = "<Display name of your received share>";
//Use if using a service principal to update the share
private static bool UseServiceTokenCredentials = false;
private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";
// General Configs
private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: UpdateReceivedShare - START");
await Receiver_UpdateReceivedShare();
Console.WriteLine($"{DateTime.Now.ToShortTimeString()}: UpdateReceivedShare - FINISH");
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
private static async Task<BinaryData> Receiver_UpdateReceivedShare()
{
TokenCredential receiverCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });
ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);
if (receivedSharesClient == null)
{
throw new InvalidEnumArgumentException("Invalid Received Shares Client.");
}
var attachedReceivedShareData = new
{
shareKind = "InPlace",
properties = new
{
displayName = ReceivedShareDisplayName,
sink = new
{
storeKind = ReceiverStorageKind,
properties = new
{
containerName = ReceiverStorageContainer,
folder = ReceiverTargetFolderName,
mountPath = ReceiverTargetMountPath
},
storeReference = new
{
referenceName = ReAttachStorageResourceId,
type = "ArmResourceReference"
}
}
}
};
var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();
var myReceivedShare = allReceivedShares.First(recShareDoc =>
{
var doc = JsonDocument.Parse(recShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
});
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("My Received Share Id: " + JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString());
Console.ForegroundColor = Console.ForegroundColor;
var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();
var receivedShare = await receivedSharesClient.CreateOrReplaceReceivedShareAsync(WaitUntil.Completed, ReceivedShareId, RequestContent.Create(attachedReceivedShareData));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(receivedShare.Value);
Console.ForegroundColor = Console.ForegroundColor;
return receivedShare.Value;
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
删除已接收的共享
此脚本删除已接收的共享。 若要使用它,请务必填写以下变量:
- ReceiverTenantId - 接收共享数据的用户/服务的 Azure 租户 ID。
- ReceiverPurviewAccountName - 将在其中接收数据的 Microsoft Purview 帐户的名称。
- ReceivedShareDisplayName - 已接收共享的显示名称。
- ReceiverStorageResourceId - 共享数据的 存储帐户的资源 ID 。
- UseServiceTokenCredentials - (可选) 如果你想要使用服务主体接收共享,请将此项设置为 true。
- ReceiverClientId - (可选) 如果使用服务主体接收共享,则这是服务主体的应用程序 (客户端) ID。
- ReceiverClientSecret - (可选) 如果使用服务主体接收共享,请添加客户端密码/身份验证密钥。
-
ReceiverPurviewEndPoint - 如果你使用的是经典 Microsoft Purview 体验,请使用
$"https://{ReceiverPurviewAccountName}.purview.azure.com";如果使用的 是新的 Microsoft Purview 体验,请使用https://api.purview-service.microsoft.com/
using Azure;
using Azure.Analytics.Purview.Sharing;
using Azure.Core;
using Azure.Identity;
using System.Text.Json;
public static class PurviewDataSharingQuickStart
{
// [REQUIRED INPUTS] Set To Actual Values.
private static string ReceiverTenantId = "<Receiver Indentity's Tenant ID>";
private static string ReceiverPurviewAccountName = "<Receiver Purview Account Name>";
private static string ReceivedShareDisplayName = "<Display name of your received share>";
private static string ReceiverStorageResourceId = "<Storage Account Resource Id for the account where the share is currently attached.>";
//Use if using a service principal to delete share.
private static bool UseServiceTokenCredentials = false;
private static string ReceiverClientId = "<Receiver Caller Application (Client) Id>";
private static string ReceiverClientSecret = "<Receiver Caller Application (Client) Secret>";
// General Configs
private static string ReceiverPurviewEndPoint = $"https://{ReceiverPurviewAccountName}.purview.azure.com";
private static string ReceiverShareEndPoint = $"{ReceiverPurviewEndPoint}/share";
private static async Task Main(string[] args)
{
try
{
TokenCredential receiverCredentials = UseServiceTokenCredentials
? new ClientSecretCredential(ReceiverTenantId, ReceiverClientId, ReceiverClientSecret)
: new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri("https://login.windows.net"), TenantId = ReceiverTenantId });
ReceivedSharesClient? receivedSharesClient = new ReceivedSharesClient(ReceiverShareEndPoint, receiverCredentials);
var allReceivedShares = await receivedSharesClient.GetAllAttachedReceivedSharesAsync(ReceiverStorageResourceId).ToResultList();
var myReceivedShare = allReceivedShares.First(recShareDoc =>
{
var doc = JsonDocument.Parse(recShareDoc).RootElement;
var props = doc.GetProperty("properties");
return props.GetProperty("displayName").ToString() == ReceivedShareDisplayName;
});
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("My Received Share Id: " + JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString());
Console.ForegroundColor = Console.ForegroundColor;
var ReceivedShareId = JsonDocument.Parse(myReceivedShare).RootElement.GetProperty("id").ToString();
await receivedSharesClient.DeleteReceivedShareAsync(WaitUntil.Completed, ReceivedShareId);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Delete Complete");
Console.ForegroundColor = Console.ForegroundColor;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex);
Console.ForegroundColor = Console.ForegroundColor;
}
}
public static async Task<List<T>> ToResultList<T>(this AsyncPageable<T> asyncPageable)
{
List<T> list = new List<T>();
await foreach (T item in asyncPageable)
{
list.Add(item);
}
return list;
}
}
清理资源
若要清理为快速入门创建的资源,请使用以下准则:
- 在 Microsoft Purview 门户中, 删除已发送的共享。
- 同时 删除收到的共享。
- 成功删除共享后,请删除收到共享数据时在目标存储帐户中创建的目标容器和 Microsoft Purview 文件夹。