如何将大型文件上载到托管Azure应用服务中的Blob存储

8e2ybdfx  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(103)

解决方案/代码如何使用.Net Core将大小超过100 MB的大文件上传到Azure托管应用服务(webapi)中的blob存储,但从本地计算机而不是Azure应用服务可以正常工作。
显示文件太大而无法上载时出错

尝试以下示例-

[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()
}
shyt4zoc

shyt4zoc1#

我在我的环境中尝试,得到以下结果:

  • 若要将大型文件从本地存储区上载到Azure Blob存储区或文件存储区,可以使用**Azure data movement library**,它为上载和下载较大文件提供高性能。*
    代码:
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();
    }
}

控制台:

门户网站:

  • 在我执行了上面的代码,并得到成功上传大文件到Azure blob存储.*

相关问题