简体   繁体   中英

How to upload large files to blob storage in hosted azure app service

solution/code how to upload large files more than 100 mb of size to blob storage in azure hosted app service (webapi) using.Net Core, But from local machine it's working not from azure app service.

Error showing file is too large to upload

Tried like one Example below -

[RequestFormLimits(MultipartBodyLengthLimit = 6104857600)]
[RequestSizeLimit(6104857600)]
public async Task<IActionResult> Upload(IFormfile filePosted)
{
    string fileName = Path.GetFileName(filePosted.FileName);
    string localFilePath = Path.Combine(fileName);
    var fileStream = new FileStream(localFilePath, FileMode.Create);
    MemoryStream ms = new MemoryStream();
    filePosted.CopyTo(ms);
    ms.WriteTo(fileStream);   
    BlobServiceClient blobServiceClient = new BlobServiceClient("ConnectionString");
    var containerClient = new blobServiceClient.GetBlobContainerClient("Container");   
   BlobUploadOptions options = new BlobUploadOptions
   {
      TransferOptions = new StorageTransferOptions
     {
        MaximumConcurrency = 8,
        MaximumTransferSize = 220 * 1024 * 1024
     }
   }
    Blobsclient bc = containerClient.GetBlobClient("Name");
    await bc.UploadAsync(fileStream, options);
   ms.Dispose();
   return Ok()
}

I tried in my environment and got below results:

To upload large files from local storage to Azure blob storage or file storage, you can use Azure data movement library ,It provides high-performance for uploading, downloading larger files.

Code:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.DataMovement;

class program
{
    public static void Main(string[] args)
    {
        string storageConnectionString = "<Connection string>";
        CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
        CloudBlobClient blobClient = account.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("test");
        blobContainer.CreateIfNotExists();
        string sourceBlob = @"C:\Users\download\sample.docx";
        CloudBlockBlob destPath = blobContainer.GetBlockBlobReference("sample.docx");
        TransferManager.Configurations.ParallelOperations = 64;
        // Setup the transfer context and track the download progress
        SingleTransferContext context = new SingleTransferContext
        {
            ProgressHandler = new Progress<TransferStatus>(progress =>
            {
                Console.WriteLine("Bytes Upload: {0}", progress.BytesTransferred);
            })
        };
        // download thee blob
        var task = TransferManager.UploadAsync(
        sourceBlob, destPath, null, context, CancellationToken.None);
        task.Wait();
    }
}

Console:

在此处输入图像描述

Portal:

After I executed the above code and got successfully uploaded large file to azure blob storage.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM