Azure搜索Blob中的元数据 [英] Azure Searching Metadata in blobs
问题描述
我正在尝试找到一种方法,以仅带回与特定数据匹配的元数据的blob存储中的项目.所有字段都有一个名为"FlightNo"的键.
I am try to find a way to bring back only items in blob storage with metadata that matches a particular piece of data. All fields will have a key called 'FlightNo'.
我真正想要的是一种查找包含与元数据匹配的所有文件(listBlob)的方法,因此,上一层,然后遍历该组数据,并查找进一步的匹配项,因为每个文件有5个项元数据.
What I want really want is a way to find all files (listBlobs) that contain a match to the metadata, so one level up, then iterate through that set of data, and find further matches as each file has 5 items of metadata.
这是迄今为止我非常不友好的代码.
Here is my very unfriendly code to date.
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();
}
}
谢谢.斯科特
推荐答案
如果我正确理解您要搜索包含所有5个元素的blob,那么您提到了项元数据.您可以使用下面的代码来做到这一点.我在我这边对其进行测试,它可以正常工作.
If I understand correctly that you want to search the blobs that contain all of 5 you mentioned items metadata. You could use the following code to do that. I test it on my side, it works correctly.
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);
}
这篇关于Azure搜索Blob中的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!