Azure Blob存储:DownloadToByteArray VS DownloadToStream [英] Azure Blob storage: DownloadToByteArray VS DownloadToStream

查看:38
本文介绍了Azure Blob存储:DownloadToByteArray VS DownloadToStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Azure Blob存储服务来在要托管在Azure网页中的网页上下文中保存/恢复文件.

I have been playing with the Azure Blob Storage service to save/recover files in a context of a web page to be hosted in Azure Web Pages.

在学习过程中,我提供了两种解决方案:第一个基本上使用 DownloadToStream 进行相同的操作,但使用 FileStream .在这种情况下,我必须先在服务器中写入文件,然后才能将其返回给用户.

During the learning process I have come with two solutions; the first basically uses DownloadToStream which does the same but with a FileStream. In this case I have to write the file in the server prior to return it to the user.

public static Stream GetFileContent(string fileName, HttpContextBase context)
{
      CloudBlobContainer container = GetBlobContainer();    
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);                                       
      Stream fileStream = new FileStream(
          context.Server.MapPath("~/App_Data/files/" + fileName), FileMode.Create);   
      blockBlob.DownloadToStream(fileStream);
      fileStream.Close();    
      return File.OpenRead(context.Server.MapPath("~/App_Data/files/" + fileName));
}

public ActionResult Download(string fileName)
{
    byte[] fileContent = MyFileContext.GetFileContent(fileName);
    return File(fileContent, "application/zip", fileName);        
}

另一方面,我使用 DownloadToByteArray 函数将Blob的内容写入以Blob文件大小初始化的字节数组中.

On the other hand I used the DownloadToByteArray function with writes the content of the Blob in an array of bytes initialized with the size of the Blob file.

public static byte[] GetFileContent(string fileName)
{
    CloudBlobContainer container = GetBlobContainer();           
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
    blockBlob.FetchAttributes();
    long fileByteLength = blockBlob.Properties.Length;
    byte[] fileContent = new byte[fileByteLength];
    for (int i = 0; i < fileByteLength; i++)
    {
        fileContent[i] = 0x20;
    }
    blockBlob.DownloadToByteArray(fileContent,0);
    return fileContent;
}

public ActionResult Download(string fileName)
{   
   byte[] fileContent = MyFileContext.GetFileStream(fileName);
   return File(fileContent, "application/zip", fileName);
}

当我同时查看这两个选项时,我看到第一个需要在服务器磁盘上创建文件,而第二个需要将Blob中的数据存储在占用内存的字节数组中.在我的特殊情况下,我将处理〜150 MB的文件大小.

When I look at both options I see the first needs to create a file in the server's disk whereas the second stores the data from the Blob in a byte array consuming memory. In my particular case I am going to handle file sizes of ~150 MB.

鉴于情况(环境,文件大小...),您认为哪种方法最好?

Given the circumstances (environment, file sizes...) which approach do you think is best?

推荐答案

Stream的好处是,您可以在下载位时逐位处理它们,而不用建立一个大字节[]然后对其进行操作完整的东西.使用Stream并没有真正获得好处,因为您正在写入文件,然后将整个文件读到内存中.流API的一个好用法是将下载流直接传送到请求的响应流,如此处答案所示

The benefit of Stream is that you can deal with bits piece-by-piece as they are downloaded rather than building up a big byte[] and then operating on the full thing. Your use of Stream isn't really getting the benefits since you are writing to a file and then reading that full file into memory. A good use of the stream API would be to pipe the download stream directly to the request's response stream as shown in the answer here Downloading Azure Blob files in MVC3

这篇关于Azure Blob存储:DownloadToByteArray VS DownloadToStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆