gpt4 book ai didi

c# - container.ListBlobs 给出了 CloudBlobDirectory 的列表 我期待的是 CloudBlockBlobs 的列表?

转载 作者:太空狗 更新时间:2023-10-29 18:20:03 24 4
gpt4 key购买 nike

我正在使用 container.ListBlobs,但它似乎正在返回一个列表 {Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.LazyEnumerable} 但是当我执行 foreach 时,对象似乎是 CloudBlobDirectory 而不是列表CloudBlockBlob。我做错了什么,还是它应该返回什么?有什么方法可以只获取 blob 列表,而不是 blob 目录?

var storageAccount = CloudStorageAccount.Parse(conn);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
Console.WriteLine(blob.GetType().ToString());
}

最佳答案

根据MSDN对于 CloudBloblContainer.ListBlobs():

The types of objects returned by the ListBlobs method depend on the type of listing that is being performed. If the UseFlatBlobListing property is set to true, the listing will return an enumerable collection of CloudBlob objects. If UseFlatBlobListing is set to false (the default value), the listing may return a collection containing CloudBlob objects and CloudBlobDirectory objects. The latter case provides a convenience for subsequent enumerations over a virtual blob hierarchy.

因此,如果您只需要 blob,则必须将 UseFlatBlobListing 属性选项设置为 true。

var storageAccount = CloudStorageAccount.Parse(conn);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
// ** new code below ** //
BlobRequestOptions options = new BlobRequestOptions();
options.UseFlatBlobListing = true;
// ** new code above ** //
var blobs = container.ListBlobs(options); // <-- add the parameter to overload
foreach (var blob in blobs)
{
Console.WriteLine(blob.GetType().ToString());
}

关于c# - container.ListBlobs 给出了 CloudBlobDirectory 的列表 我期待的是 CloudBlockBlobs 的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25910519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com