ListBlobs方法懒惰地检索该容器中的 blob 。因此,您可以针对该方法编写查询,这些查询在您循环(或使用 ToList 或其他方法实现对象)列表之前不会执行。
举几个例子,事情就会变得更清楚。对于那些不知道如何在 Azure 存储帐户中获取容器引用的人,我推荐本教程。
按最后修改日期排序并获取第 2 页(每页 10 个 blob):
blobContainer.ListBlobs().OfType<CloudBlob>()
.OrderByDescending(b=>b.Properties.LastModified).Skip(10).Take(10);
获取特定类型的文件。如果您在上传时设置了 ContentType(我强烈建议您这样做),这将起作用:
blobContainer.ListBlobs().OfType<CloudBlob>()
.Where(b=>b.Properties.ContentType.StartsWith("image"));
获取 .jpg 文件并按文件大小排序,假设您设置文件名及其扩展名:
blobContainer.ListBlobs().OfType<CloudBlob>()
.Where(b=>b.Name.EndsWith(".jpg")).OrderByDescending(b=>b.Properties.Length);
最后,查询不会被执行,直到你告诉它:
var blobs = blobContainer.ListBlobs().OfType<CloudBlob>()
.Where(b=>b.Properties.ContentType.StartsWith("image"));
foreach(var b in blobs) //This line will call the service,
//execute the query against it and
//return the desired files
{
// do something with each file. Variable b is of type CloudBlob
}