Search code examples
azureazure-blob-storageazure-storage

How to Upload file in Azure Blob for file size greater than 1 GB in asp.net using FileUpload


I am able to upload files in ASP.NET WebForm 4.8. I have updated the settings in webconfig for max file size. The problem I am getting when streaming the file to Azure Storage blob. I get outofmemoery exception when loading file greater than 1 Gb and less then 2 GB. I mentioned less than 2 GB because I know ASP.NET cannot load the file which is greater then 2 GB.

If someone can also provide information when to use blobClient.OpenWrite() vs blobClient.Upload()


Solution

  • How to Upload file in Azure Blob for file size greater than 1 GB in asp.net using FileUpload.

    I used the below code and able to upload a file of 1.23GB to a container.

    int blockSize = 4 * 1024 * 1024;
    BlobServiceClient blbSvcClnt = new BlobServiceClient("ConnectionString");
    BlobContainerClient contClnt = blbSvcClnt.GetBlobContainerClient("mycntr11");
    BlockBlobClient blbClnt = contClnt.GetBlockBlobClient("MyFiles11.zip");
    FileStream fstream = File.OpenRead("C:\\Tools\\MyFiles11.zip");            ArrayList blockIDArrayList = new ArrayList();
    byte[] buffer;
    var bytesLeft = (fstream.Length - fstream.Position);
    while (bytesLeft > 0)
          {
            if (bytesLeft >= blockSize)
               {
                  buffer = new byte[blockSize];
                  await fstream.ReadAsync(buffer, 0, blockSize);
                }
            else
               {
                     buffer = new byte[bytesLeft];
                     await fstream.ReadAsync(buffer, 0, Convert.ToInt32(bytesLeft));
                     bytesLeft = (fstream.Length - fstream.Position);
               }
           using (var stream = new MemoryStream(buffer))
            {
               string blockID = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
                blockIDArrayList.Add(blockID);
                await blbClnt.StageBlockAsync(blockID, stream);
             }
              bytesLeft = (fstream.Length - fstream.Position);
             }
             string[] blockIDArray = (string[])blockIDArrayList.ToArray(typeof(string));
             await blbClnt.CommitBlockListAsync(blockIDArray);
    
    

    1.23 GB file uploaded successfully to azure container.

    enter image description here

    Local File.

    enter image description here

    When to use blobClient.OpenWrite() vs blobClient.Upload()

    File Size

    • blobClient.OpenWrite(): This is suitable for larger files, when the file size exceeds the available memory. It allows you to upload the file in smaller blocks, reducing the memory footprint during the upload process.
    • blobClient.Upload(): This is suitable for smaller files that can be easily loaded into memory without causing memory constraints.

    Memory Usage

    • blobClient.OpenWrite(): Since this uploads the file in smaller blocks, it minimizes the memory usage during the upload process. It can be useful with limited memory resources or when uploading very large files.

    For more information, refer to the MSDoc1 and MSDoc2. How to Upload file in Azure Blob for file size greater than 1 GB in asp.net using FileUpload.