Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadToStreamAsync returns empty stream C#

I'm trying to download a blob file as a bytearray from my blob account on Azure. I do this.

 var blockBlob = blobContainer.GetBlockBlobReference(id);

                using (var mStream = new MemoryStream())
                {
                    blockBlob.DownloadToStreamAsync(mStream);
                    result = mStream.ToArray();
                }

The code above returns me a empty stream. I do have the file on my azure blob account and I checked the uri that is generated by code and it's the same that the one on my azure blob for the file I want to download as bytearray.

Is there a better way to download a azure blob file as bytearray in c#?

like image 358
Victor A Chavez Avatar asked Feb 28 '26 03:02

Victor A Chavez


1 Answers

You should wait till the async operation gets completed.

          using (var mStream = new MemoryStream())
            {

                 await  blockBlob.DownloadToStreamAsync(mStream);
                 result = mStream.ToArray();
            }

You are getting empty because you try to get value from it when it is not yet available.

like image 114
debojyoti talukdar Avatar answered Mar 02 '26 19:03

debojyoti talukdar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!