简体   繁体   English

转换 Azure.AsyncPageable<blobitem> 列出<blobitem></blobitem></blobitem>

[英]Convert Azure.AsyncPageable<BlobItem> to List<BlobItem>

I am retrieving lakhs of blobs from Azure blob storage container.我正在从 Azure blob 存储容器中检索数十万个 blob。 Based on the retrieval I am calculating the retrieval time and need to convert the blobs as readable format and need to get the blob items count in the result.基于检索,我正在计算检索时间,需要将 blob 转换为可读格式,并且需要获取结果中的 blob 项目数。 Hence I am retrieving the blobs and converting it into List< BlobItem > as mentioned below.因此,我正在检索 blob 并将其转换为如下所述的List< BlobItem > Now the problem is, when I retrieve all the blobs available in the container using the method GetBlobsAsync() , it is taking few milliseconds to get all the blob items from the storage container.现在的问题是,当我使用GetBlobsAsync()方法检索容器中所有可用的 blob 时,需要几毫秒才能从存储容器中获取所有 blob 项。 But when am converting the blob items into List< BlobItem > using blobItems.ToListAsync() , it is taking 5-10 minutes to complete it.但是当我使用blobItems.ToListAsync()将 blob 项目转换为List< BlobItem >时,需要 5-10 分钟才能完成。

var watch = System.Diagnostics.Stopwatch.StartNew();
BlobContainerClient client = new BlobContainerClient(connectionString, containerName);
var blobItems =  client.GetBlobsAsync(); //This line take 60-80 milli seconds to get all blobs
var blobList =await blobItems.ToListAsync(); //This line takes 5-10mins to complete
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
return new OkObjectResult(blobList);

During the conversion only it is taking too long to complete all the blobs.仅在转换期间完成所有 blob 花费的时间太长。 Even I tried the same with Foreach loop, it is also taking long time(5-10 mins) to fetch the entire data as readable data.即使我对 Foreach 循环进行了同样的尝试,它也需要很长时间(5-10 分钟)才能将整个数据作为可读数据获取。 Need help on converting Azure.AsynPageable to some other readable format within some milliseconds.需要帮助在几毫秒内将Azure.AsynPageable转换为其他可读格式。 If there will be any other ways, we can achieve the same also fine.如果还有其他方法,我们也可以实现同样的效果。 Thanks谢谢

Now the problem is, when I retrieve all the blobs available in the container using the method GetBlobsAsync(), it is taking few milliseconds to get all the blob items from the storage container.现在的问题是,当我使用 GetBlobsAsync() 方法检索容器中所有可用的 blob 时,需要几毫秒才能从存储容器中获取所有 blob 项。 But when am converting the blob items into List< BlobItem > using blobItems.ToListAsync(), it is taking 5-10 minutes to complete it.但是当我使用 blobItems.ToListAsync() 将 blob 项目转换为 List< BlobItem > 时,需要 5-10 分钟才能完成。

This is not what is happening.这不是正在发生的事情。 GetBlobsAsync returns an AsyncPageable . GetBlobsAsync返回一个AsyncPageable It does not actually fetch the blob items.它实际上并不获取 blob 项目。 That only happens when you iterate over the result, as stated in the docs文档中所述,只有在迭代结果时才会发生这种情况

The GetBlobsAsync(BlobTraits, BlobStates, String, CancellationToken) operation returns an async sequence of blobs in this container. GetBlobsAsync(BlobTraits, BlobStates, String, CancellationToken) 操作返回此容器中 blob 的异步序列。 Enumerating the blobs may make multiple requests to the service while fetching all the values.枚举 blob 可能会在获取所有值时向服务发出多个请求。

In your case, the code await blobItems.ToListAsync();在您的情况下,代码await blobItems.ToListAsync(); enumerates the blobs so that will take most of the time.枚举 blob,因此这将花费大部分时间。

Got your point.明白你的意思了。 But await blobItems.ToListAsync();但是等待 blobItems.ToListAsync(); is taking long time to process the data retrieval.处理数据检索需要很长时间。 Is there any way to speedup the retrieve process by using any parallel programing process or any other way?有没有办法通过使用任何并行编程过程或任何其他方式来加速检索过程?

If you have a virtual folder structure you could fetch blob items in batches using the prefix parameter of the GetBlobsAsync method like this:如果您有虚拟文件夹结构,则可以使用GetBlobsAsync方法的prefix参数批量获取 blob 项目,如下所示:

async Task DownloadBlobsAsync(BlobContainerClient client)
{
    var blobItemLists = await Task.WhenAll(new[] {
        DownloadBlobsByPrefix("folder1"),
        DownloadBlobsByPrefix("folder2")
    });
    
    var  blobItems = blobItemLists.SelectMany(items => items);
}

async Task<List<BlobItem>> DownloadBlobsByPrefix(string prefix)
{
    var blobItems = client.GetBlobsAsync(prefix: prefix);
    return await blobItems.ToListAsync();
}

but you will need to measure to see if this has the effect you want.但是你需要测量看看这是否有你想要的效果。 Also, you need to think about the right set of prefixes to use.此外,您需要考虑使用正确的前缀集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM