0
votes

I want to download files from Azure using C# then stream those into MemoryStream after that return/display to the user in Front-end with a link (Azure URI - which goes to the Azure blob) and the user will be able to see those PDF files in the browser or download them. There are multiple blobs/files in Azure so, I want to loop through each file and download to stream for example: using a foreach.

I'm not sure how can I reference those blobs CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); as here I could give a name of the specific file but I've multiple files so not sure what to go here "fileName".

Code:

var files = container.ListBlobs();

foreach (var file in files)
{
   using (var memoryStream = new MemoryStream())
   {
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
      blockBlob.DownloadToStream(memoryStream);
   }
}

I'm not sure if I'm looping correcting right now in the code and downloading every blob?

Also, I tried replacing fileName with file.Uri.Segments.Last() - I guess which gets the name of blobs.

The problem I'm having is that this foreach is just getting me one PDF file whenever I try to use the links in front-end. So, I need to know how can I properly loop through each file and download them?

1
@ScottChamberlain - Actually, after this works I was thinking to use SAS so that user can only use those links for short period of time as the container has read access only, however, if you've some other way which would be better let me know. - Pathankotia
I've got a method through which I'll return the byte[] of each blob to the user with content-type & file name so, first I want to download to stream then convert these files into byte[] and then return. - Pathankotia
You have not told us what goes wrong when you try to do the code you posted. Are you getting a error, unexpected behavior, what? Please edit your question to add in the details. - Scott Chamberlain
The problem you are stating does not do anything with the files other than copy it to a memory stream then throw it away, how do you know only one file is being read? You need to create a Minimal, Complete, and Verifiable example that demonstrates the problem that we could copy and paste and see in our own projects. - Scott Chamberlain
Are you sure you're uploading the file correctly? And its not zero bytes? - granadaCoder

1 Answers

1
votes

So, I need to know how can I properly loop through each file and download them?

We can't download the mutiple files from the memory directly. If zip file is acceptable, you could use a compressed file such as a zip file to transfer multiple files instead. The following is my demo code, it works correctly on my side.

  using (var ms = new MemoryStream())
  {
      using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
      {
           foreach (var file in files)
           {
                if (file.GetType() != typeof(CloudBlockBlob)) continue;
                var blob = (CloudBlockBlob) file;
                var entry = zipArchive.CreateEntry(blob.Name, CompressionLevel.Fastest);
                using (var entryStream = entry.Open())
                {
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob.Name);
                    blockBlob.DownloadToStream(entryStream);

                }
           }
      }
  }