sco*_*dro 5 c# azure azure-blob-storage
我试图找到一种方法,只使用与特定数据匹配的元数据来恢复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();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.斯科特
Fis*_*ake 15
接受的答案非常低效,循环并加载每个Blob及其关联的元数据,以检查值对于任何合理数据量都不会很好地执行.
可以使用Azure搜索搜索Blob元数据.可以创建包括Blob自定义元数据的搜索索引.
以下综合文章解释了这一切:
使用Azure搜索在Azure Blob存储中索引文档使用Azure搜索
搜索Blob存储
如果我理解正确的话,您想要搜索包含您提到的所有 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4466 次 |
| 最近记录: |