列出C#中具有特定元数据的所有Blob



我有一个blob容器,其中有一个自定义元数据"结果";对于所有Blob都设置为True,并且由于一些糟糕的代码编写,一些Blob没有设置这些元数据。我想列出所有没有";结果";作为元数据;结果";元数据值不是True。有可能写一个c#代码来进行这样的过滤并返回Blob列表吗?我在网上找不到任何例子。

我刚开始写代码,希望在完成它时得到一些帮助。

var blobAccount = "<account>";
var apiKey = "<api-key>";
var containerName = "<container>";
var storageCredentials = new StorageCredentials(blobAccount, apiKey);
var account = new CloudStorageAccount(storageCredentials, true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);

关于这个问题,请参考以下代码

var blobAccount = "<account>";
var apiKey = "<api-key>";
var containerName = "<container>";
var storageCredentials = new StorageCredentials(blobAccount, apiKey);
var account = new CloudStorageAccount(storageCredentials, true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
BlobContinuationToken continuationToken = null;
var segmentSize = 20;
List<CloudBlob> blobs = new List<CloudBlob>();
try
{
// Call the listing operation and enumerate the result segment.
// When the continuation token is null, the last segment has been returned
// and execution can exit the loop.
do
{
BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(string.Empty,
true, BlobListingDetails.Metadata, segmentSize, continuationToken, null, null);
// get the blob which its metadata does not container one 
var r = resultSegment.Results.OfType<CloudBlob>().Where(b => !b.Metadata.ContainsKey("type")).ToList();
blobs.AddRange(r);

Console.WriteLine();
// Get the continuation token and loop until it is null.
continuationToken = resultSegment.ContinuationToken;
} while (continuationToken != null);
}
catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}

最新更新