title: How to improve performance of downloading large size azure blob file over a stream?
body: <p>I have JSON blob file of size around 212 MB. <br>
  On Local while debugging it is taking around 15 minutes to download.<br>
  When i deploy code to Azure app service it runs for 10 minutes and fails with error : (locally it fails intermittently with same error)</p>
  
  <blockquote>
    <p>Server failed to authenticate the request. Make sure the value of
    Authorization header is formed correctly including the signature</p>
  </blockquote>
  
  <p>Code Attempt 1:</p>
  
  <pre><code>// Create SAS Token for referencing a file for a duration of 5 min
  SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy
  {
      SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15),
      Permissions = SharedAccessBlobPermissions.Read
  };
  
  var blob = cloudBlobContainer.GetBlockBlobReference(blobFilePath);
  string sasContainerToken = blob.GetSharedAccessSignature(sasConstraints);
  
  var cloudBlockBlob = new CloudBlockBlob(new Uri(blob.Uri + sasContainerToken));
  
  using (var stream = new MemoryStream())
  {
       await cloudBlockBlob.DownloadToStreamAsync(stream);
      //resetting stream's position to 0
  
      stream.Position = 0;
      var serializer = new JsonSerializer();
  
      using (var sr = new StreamReader(stream))
      {
          using (var jsonTextReader = new JsonTextReader(sr))
          {
              jsonTextReader.SupportMultipleContent = true;
              result = new List&lt;T&gt;();
              while (jsonTextReader.Read())
              {
                  result.Add(serializer.Deserialize&lt;T&gt;(jsonTextReader));
              }
          }
      }
  }
  </code></pre>
  
  <p>Code Attempt 2: I have tried using DownloadRangeToStreamAsync for downloading a blob in chunk but nothing changed :</p>
  
  <pre><code>int bufferLength = 1 * 1024 * 1024;//1 MB chunk
  long blobRemainingLength = blob.Properties.Length;
  Queue&lt;KeyValuePair&lt;long, long&gt;&gt; queues = new Queue&lt;KeyValuePair&lt;long, long&gt;&gt;();
  long offset = 0;
  do
  {
      long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
  
      offset += chunkLength;
      blobRemainingLength -= chunkLength;
      using (var ms = new MemoryStream())
      {
          await blob.DownloadRangeToStreamAsync(ms, offset, chunkLength);
          ms.Position = 0;
          lock (outPutStream)
          {
              outPutStream.Position = offset;
              var bytes = ms.ToArray();
              outPutStream.Write(bytes, 0, bytes.Length);
          }
      }
  }
  while (blobRemainingLength &gt; 0);
  </code></pre>
  
  <p>I think 212 MB data is not a large JSON file. Can you please suggest a
  solution ?</p>