简体   繁体   English

如何使用 Blazor 将大文件上传到 WebAPI (C#) 并将其存储到 Azure Blob 存储中

[英]How to upload large file using Blazor to WebAPI (C#) and store it into Azure Blob storage

I need to upload a large file(up 4GB) using Blazor. Please in your answers using the new version of Azure Storage SDK (v12 Azure.Storage.Blobs NuGet package.)我需要使用 Blazor 上传一个大文件(最大 4GB)。请在您的回答中使用新版本的 Azure 存储 SDK(v12 Azure.Storage.Blobs NuGet package。)

In my case, the old version will not work Azure Storage SDK (v11 WindowsAzure.Storage NuGet package.)就我而言,旧版本将不起作用 Azure 存储 SDK (v11 WindowsAzure.Storage NuGet package。)

Requirement`要求`

  1. Upload file in Blazor UI在Blazor UI上传文件
  2. Send that file to the server side via API通过 API 将该文件发送到服务器端
  3. Store that file in Azure Blob Storage on the server side.将该文件存储在服务器端的 Azure Blob 存储中。

I try a few ways to do this but I didn't manage it.我尝试了几种方法来做到这一点,但我没有成功。 Is there any way to do this?有什么办法吗?

What I tried`我试过的`

  1. Send the file itself(2GB) to API but out of memory exception (google says do not do this way, bad practice)将文件本身 (2GB) 发送到 API 但出现 memory 异常(谷歌说不要这样做,不好的做法)

2.Cut the file as chunks and send it to API, the first part of sending works but I can't collect them all together as one file and store it in Azure Blob Storage (I will have a memory issue). 2.将文件切割成块并将其发送到 API,这是发送工作的第一部分,但我无法将它们全部收集为一个文件并将其存储在 Azure Blob 存储中(我将遇到 memory 问题)。

If there is a way to store it in Azure Blob for example store the first part of chunks then update that file and write other parts sequentially in one file on Azure Blob not service memory. In the end, I will have one large file in Blob.如果有办法将它存储在 Azure Blob 中,例如存储块的第一部分,然后更新该文件并依次将其他部分写入 Azure Blob 上的一个文件中,而不是服务 memory。最后,我将在 Blob 中有一个大文件.

If there is a way to store it in Azure Blob for example store the first part of chunks then update that file and write other parts sequentially in one file on Azure Blob not service memory.如果有办法将其存储在 Azure Blob 中,例如存储块的第一部分,然后更新该文件并将其他部分顺序写入 Azure Blob 上的一个文件中,Blob 不服务 memory。

Yes.是的。 This is how Block Blobs work in Azure Storage.这就是Block Blobs在 Azure 存储中的工作方式。

Basically what you will need to do is send chunks (called blocks in Azure Storage) of the data to your API and directly save those chunks in blob storage using BlockBlobClient.StageBlockAsync .基本上您需要做的是将数据块(在 Azure 存储中称为blocks )发送到您的 API 并使用BlockBlobClient.StageBlockAsync将这些块直接保存在 blob 存储中。 There is no need for you to save those chunks in your API. You will need to maintain the block ids of the chunks you are uploading in your Blazor application.您无需将这些块保存在您的 API 中。您需要维护您在 Blazor 应用程序中上传的块的块 ID。

Once all the chunks (blocks) are uploaded, you will need to send the block ids to your API and will need to call BlockBlobClient.CommitBlockListAsync from your API to tell Azure Blob Storage to combine all blocks together to create a blob.上传所有块(块)后,您需要将块 ID 发送到您的 API,并且需要从您的 API 调用BlockBlobClient.CommitBlockListAsync以告知 Azure Blob 存储将所有块组合在一起以创建一个 blob。

To learn more about creating block blobs by uploading blocks, you may find these links useful:要了解有关通过上传块创建块 blob 的更多信息,您可能会发现这些链接很有用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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