我正在尝试找到一种方法,在blob存储中仅带回具有与特定数据片段匹配元数据的项目。所有字段都将有一个名为“FlightNo”的键。
我真正想要的是一种找到包含元数据匹配的所有文件(listBlobs)的方法,因此向上一层,然后迭代该数据集,并找到进一步的匹配,因为每个文件都有5个元数据项。
这是我迄今为止非常不友好的代码。
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
blob.FetchAttributes();
foreach (var metaDataItem in blob.Metadata)
{
dictionary.Add(metaDataItem.Key, metaDataItem.Value);
}
if (dictionary.Where(r=>r.Key == "FlightNo" && r.Value == FlightNo).Any())
{
if (dictionary.Where(r => r.Key == "FlightDate" && r.Value == FlightDate).Any())
{
if (dictionary.Where(r => r.Key == "FromAirport" && r.Value == FromAirport).Any())
{
if (dictionary.Where(r => r.Key == "ToAirport" && r.Value == ToAirport).Any())
{
if (dictionary.Where(r => r.Key == "ToAirport" && r.Value == ToAirport).Any())
{
retList.Add(new BlobStorage()
{
Filename = blob.Name,
BlobType = blob.BlobType.ToString(),
LastModified = (DateTimeOffset)blob.Properties.LastModified,
ContentType = blob.Properties.ContentType,
Length = blob.Properties.Length,
uri = RemoveSecondary(blob.StorageUri.ToString()),
FlightNo = dictionary.Where(r => r.Key == "FlightNo").Select(r => r.Value).SingleOrDefault(),
Fixture = dictionary.Where(r => r.Key == "FixtureNo").Select(r => r.Value).SingleOrDefault(),
FlightDate = dictionary.Where(r => r.Key == "FlightDate").Select(r => r.Value).SingleOrDefault(),
FromAirport = dictionary.Where(r => r.Key == "FromAirport").Select(r => r.Value).SingleOrDefault(),
ToAirport = dictionary.Where(r => r.Key == "ToAirport").Select(r => r.Value).SingleOrDefault()
});
}
}
}
}
}
dictionary.Clear();
}
}谢谢。史考特
发布于 2017-09-29 10:59:29
如果我理解正确的话,您希望搜索包含您提到的所有5个项元数据的blob。您可以使用以下代码来完成此操作。我在我这边测试了它,它工作正常。
var connectionString = "storage connection string";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("container");
var blobs = container.ListBlobs();
var blobList = new List<CloudBlockBlob>();
foreach (var item in blobs)
{
CloudBlockBlob blob = (CloudBlockBlob)item;
blob.FetchAttributes();
if (blob.Metadata.Contains(new KeyValuePair<string, string>("FlightNo", "FlightNoValue")) &&
blob.Metadata.Contains(new KeyValuePair<string, string>("FlightDate", "FlightDateValue")) &&
blob.Metadata.Contains(new KeyValuePair<string, string>("FromAirport", "FromAirportValue")) &&
blob.Metadata.Contains(new KeyValuePair<string, string>("ToAirport", "ToAirportValue")) &&
blob.Metadata.Contains(new KeyValuePair<string, string>("FixtureNo", "FixtureNoValue")))
{
blobList.Add(blob);
}发布于 2018-06-06 16:12:56
公认的答案是非常低效的,循环和加载每个单独的Blob和它们的关联元数据来检查值对于任何合理的数据量都不会有很好的性能。
可以使用Azure Search搜索Blob元数据。可以创建包括斑点定制元数据的搜索索引。
下面的综合文章对此进行了全面的解释:
发布于 2020-05-11 09:33:59
尽管仍处于预览阶段,但使用Blob Index,您现在可以对blob元数据(标记)进行查询搜索。
在找到你想要的东西之前,你不需要遍历所有的斑点。
下面是full article中的一段代码片段
Azure Blob索引-一种托管的二级索引,允许您存储多维对象属性来描述
Blob存储的数据对象-现在可以预览。blob Index构建于Blob存储之上,可为您的所有工作负载提供一致的可靠性、可用性和性能。Blob索引提供本机对象管理和筛选功能,允许您根据数据上设置的属性标签对数据进行分类和查找。
https://stackoverflow.com/questions/46472504
复制相似问题