title: Azure Blob storage: DownloadToByteArray VS DownloadToStream
body: <p>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.</p>
  
  <p>During the learning process I have come with two solutions; the first basically uses <code>DownloadToStream</code> which does the same but with a <code>FileStream</code>. In this case I have to write the file in the server prior to return it to the user. </p>
  
  <pre><code>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);        
  }
  </code></pre>
  
  <p>On the other hand I used the <code>DownloadToByteArray</code> function with writes the content of the Blob in an array of bytes initialized with the size of the Blob file.</p>
  
  <pre><code>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 &lt; 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);
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>Given the circumstances (environment, file sizes...) which approach do you think is best?</p>