how to download the large files(18 GB) from azure private storage containers in asp.core ?

Natarajan, Srikanth 20 Reputation points
2024-01-05T11:31:51.94+00:00

we have line of business to provide the files download functionality from azure storage to client accessing web application developed asp.net core mvc hosted in azure.

Downloading via Sas is working for fine only the storage account which is public. due to security compliance we have to mark the storage account under private after that donlaoding via sas url is not working

So I tried  with  another approach  -> Download the content from  container with Chunk  and Parallel options  and convert to it byte array and open  it in the browser as file output.

 

It is working fine upto 400 MB ,post that throws out of memory exception

find below the code snippets

public async Task<byte[]> DownloadFilefromStoarage(string blobName)
        {
            try
            {
                var _connectionString = "XXX"
                var _containerName = "XXX";
                var blobServiceClient = new BlobServiceClient(_connectionString);
                var containerClient = blobServiceClient.GetBlobContainerClient(_containerName);
                var blobClient = containerClient.GetBlobClient(blobName);



                var transferOptions = new StorageTransferOptions
                {
                    // Set the maximum number of parallel transfer workers
                    MaximumConcurrency = 2,
                    // Set the initial transfer length to 8 MiB
                    InitialTransferSize = 8 * 1024 * 1024,
                    // Set the maximum length of a transfer to 4 MiB
                    MaximumTransferSize = 4 * 1024 * 1024
                };

                BlobDownloadToOptions downloadOptions = new BlobDownloadToOptions()
                {
                    TransferOptions = transferOptions
                };
                var memoryStream = new MemoryStream();                      
                await blobClient.DownloadToAsync(memoryStream, downloadOptions);

                byte[] result = memoryStream.ToArray();
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,203 questions
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. KarishmaTiwari-MSFT 20,782 Reputation points Microsoft Employee Moderator
    2024-01-07T18:10:57.9933333+00:00

    @Natarajan, Srikanth Can you try this and let me know the results:

    BlobDownloadInfo download = await blobClient.DownloadAsync();      
    Stream stream = download.Content;        
    var contentType = download.ContentType;              
    return new FileStreamResult(stream, contentType)        
    {            FileDownloadName = blobFileName        
    };
    
    

    Source: https://stackoverflow.com/questions/76462212/how-to-return-azure-blob-storage-fileas-filestreamresult

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,311 Reputation points Volunteer Moderator
    2024-01-05T16:33:17.0666667+00:00

    You code buffering the blob in memory, then writing it out. you should use a FileStreamResult instead.

    0 comments No comments

  2. Yash Raj 0 Reputation points
    2025-04-17T16:27:00.3+00:00

    Hi ,@Natarajan, Srikanth
    Can you help me with frontEnd Code approach you used to download files of 10Gbs , Like this controller sends data in form of stream , how did you handled front-code to download this file


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.