CloudBlockBlob的OpenReadAsync和DownloadFromStreamAsync功能之间的区别

时间:2016-08-10 05:40:40

标签: azure azure-storage-blobs

Azure blob存储中OpenReadAsync的{​​{1}}和DownloadToStreamAsync函数之间有什么区别?在谷歌搜索但找不到答案。

3 个答案:

答案 0 :(得分:13)

OpenReadAsync和DownloadToStreamAsync都可以启动异步操作以检索blob流。 根据我的测试,您可以通过以下部分更好地了解它们:

基本概念

DownloadToStreamAsync:启动异步操作,将blob的内容下载到流中。

OpenReadAsync:启动异步操作以将blob的内容下载到流中。

用法

a)DownloadToStreamAsync

示例代码:

using (var fs = new FileStream(<yourLocalFilePath>, FileMode.Create))
{
    await blob.DownloadToStreamAsync(fs);
}

b)OpenReadAsync

示例代码:

//Set buffer for reading from a blob stream, the default value is 4MB.
blob.StreamMinimumReadSizeInBytes=10*1024*1024; //10MB
using (var blobStream = await blob.OpenReadAsync())
{
    using (var fs = new FileStream(localFile, FileMode.Create))
    {   
       await blobStream.CopyToAsync(fs);
    }
}

通过Fiddler

捕获网络请求

a)DownloadToStreamAsync

enter image description here enter image description here b)OpenReadAsync

enter image description here enter image description here

根据以上所述,DownloadToStreamAsync只发送一个获取请求以检索blob流,而OpenReadAsync根据您设置的“Blob.StreamMinimumReadSizeInBytes”或默认值发送多个请求来检索blob流。

答案 1 :(得分:0)

DownloadToStreamAsyncOpenReadAsync 的区别在于 DownloadToStreamAsync 会在返回前将 blob 的内容下载到流中,但 OpenReadAsync 不会触发下载,直到流被消耗。

例如,如果使用它从 ASP.NET 核心服务返回文件流,则应使用 OpenReadAsync 而不是 DownloadToStreamAsync

使用 DownloadToStreamAsync 的示例(在这种情况下不推荐):

Stream target = new MemoryStream(); // Could be FileStream
await blob.DownloadToStreamAsync(target); // Returns when streaming (downloading) is finished. This requires the whole blob to be kept in memory before returning!
_logger.Log(LogLevel.Debug, $"DownloadToStreamAsync: Length: {target.Length} Position: {target.Position}"); // Output: DownloadToStreamAsync: Length: 517000 Position: 517000
target.Position = 0; // Rewind before returning Stream:
return File(target, contentType: blob.Properties.ContentType, fileDownloadName: blob.Name, lastModified: blob.Properties.LastModified, entityTag: null);

使用 OpenReadAsync 的示例(在这种情况下推荐):

// Do NOT put the stream in a using (or close it), as this will close the stream before ASP.NET finish consuming it.
Stream blobStream = await blob.OpenReadAsync(); // Returns when the stream has been opened
_logger.Log(LogLevel.Debug, $"OpenReadAsync: Length: {blobStream.Length} Position: {blobStream.Position}"); // Output: OpenReadAsync: Length: 517000 Position: 0
return File(blobStream, contentType: blob.Properties.ContentType, fileDownloadName: blob.Name, lastModified: blob.Properties.LastModified, entityTag: null);

答案 2 :(得分:-1)

OpenReadAsync返回Task<Stream>,您可以使用等待。

样本测试方法

CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(wholeBlob);
                }

                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    using (var blobStream = await blob.OpenReadAsync())
                    {
                        await TestHelper.AssertStreamsAreEqualAsync(wholeBlob, blobStream);
                    }
                }
            }

DownloadToStreamAsync是一个虚拟(可以被覆盖)的方法,它返回一个任务并将流对象作为输入。

样本用法。

await blog.DownloadToStreamAsync(memoryStream);