问题描述
我一直在尝试找出连接到 Azure 存储帐户、循环访问多个容器并将容器内的 blob 转换为对象的最快方法。容器中的所有元素都是 JSON 并且匹配不同的对象。
在 Azure 存储资源管理器上看到的结构是:
Azure_Subscription
|--Storage_Accounts
|--My_Storage_Account
|--blob1
|--blob2
|--blob3
etc
现在根据我在官方文档中阅读的内容 here,要访问和下载每个 blob 并对其进行转换,以便可以将其作为 JSON 处理并反序列化,我需要执行以下所有操作(假设我没有 blob URI 列表)。
string testConnectionString = "DefaultEndpointsProtocol=https;AccountName=;AccountKey=;EndpointSuffix=core.windows.net";
// the service clients allow working at the Azure Storage level with Tables and Blobs
TableServiceClient tableServiceClient = new TableServiceClient(testConnectionString);
BlobServiceClient blobServiceClient = new BlobServiceClient(testConnectionString);
List<blob1> blob1List = new List<blob1>;
// this gives me a list of blob containers and I can programmatically retrieve
// the name of each individual container.
Pageable<BlobContainerItem> blobContainers = blobServiceClient.GetBlobContainers();
// each BlobContainerItem represents an individual blob container (bill,building...)
foreach (BlobContainerItem blobContainerItem in blobContainers)
{
// create a ContainerClient to make calls to each individual container
BlobContainerClient clientForIndividualContainer =
blobServiceClient.GetBlobContainerClient(blobContainerItem.Name);
if (blobItem.Name.Equals("blob1"))
{
Pageable<BlobItem> blobItemList = clientForIndividualContainer.GetBlobs();
foreach (BlobItem bi in blobItemList)
{
BlobClient blobClient = clientForIndividualContainer.GetBlobClient(bi.Name);
var blobContent = blobClient.Download();
StreamReader reader = new StreamReader(blobContent.Value.Content);
string text = reader.ReadToEnd();
blob1List.Add(JsonSerializer.Deserialize<blob1>(text));
}
}
}
该项目的目标是 .net 5.0,我也需要对 Azure Tables 做一些类似的事情。目标是我想查看多个容器中的所有 blob(实际上都是 JSON),并将它们与另一个存储帐户中的所有 blob 进行比较。我也愿意接受任何完全不同的想法,但这样做的目的是比较 Azure 存储 blob 的输入,并确保新进程上传相同的对象结构。因此,对于 Azure 存储帐户中的所有 blob1
项,我将它们与另一个存储帐户中所有 oldBlob1
项的列表进行比较,并检查它们是否都相等。
我希望这个问题有意义...此时上面的代码有效,我可以将 if-else
中的功能移到一个方法中,而不是 if-else
语句使用 {{1 }},但我的主要问题是完全达到这一点。如果没有大量的 blob URI 列表,我是否需要创建一个 switch
才能创建一个 BlobServiceClient
(s) 列表,然后循环遍历每个容器并为所有容器创建{ {1}}(s) 然后为存储帐户中的每个 blob 创建一个 BlobContainerItem
以最终能够访问 blob 的 BlobContainerClient
?
这似乎需要大量工作才能访问单个文件。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)