Using bytes from Azure DownloadToStreamAsync as they are read?

1.1k Views Asked by At

I'm trying to download a blob from Azure Storage using the DownloadToStreamAsync() method.

 using (MemoryStream writeStream = new MemoryStream())
 {
      await blockBlob.DownloadToStreamAsync(writeStream);
 }

I can successfully download the file, but one of the documented advantages to using this method is that you can use the data from the stream as it comes in.
How can I achieve this? How can I see the progress of the stream and, say, update the UI to show current bytes read?

1

There are 1 best solutions below

0
On

Progress reporting is a feature request we previously received, but we do not have a timeline to share at this point. In the meantime, I could suggest some workarounds:

  1. Wrap the MemoryStream and create your own Stream class. By overriding methods like Write, BeginWrite, and WriteAsync, you can count the number of bytes received.
  2. You can instead call CloudBlockBlob.OpenRead and get a Stream that internally downloads the blob. Then you can read in chunks and thus know how many bytes you have received so far. However, this has the disadvantage of sending more requests to the Blob service and therefore increasing the latency and cost.