我知道分块上传,我们必须在接收端做些什么吗?

I know about uploading in chunks, do we have to do something on receiving end?

我的 azure 函数接收大型视频文件和图像并将其存储在 Azure blob 中。客户端 API 正在将数据分块发送到我的 Azure HTTP 触发器函数。我是否必须在接收端做一些事情来提高性能,比如以块的形式接收数据?

Bruce,实际上客户端代码正在由其他团队开发。现在我正在通过邮递员对其进行测试并从多部分 http 请求中获取文件。

foreach (HttpContent ctnt in provider.Contents)  {

                var dataStream = await ctnt.ReadAsStreamAsync();
 if (ctnt.Headers.ContentDisposition.Name.Trim().Replace("\"", "") == "file")
               {                
                        byte[] ImageBytes = ReadFully(dataStream);
                        var fileName = WebUtility.UrlDecode(ctnt.Headers.ContentDisposition.FileName);                         

              } }

ReadFully 函数

 public static byte[] ReadFully(Stream input){
using (MemoryStream ms = new MemoryStream())
{
    input.CopyTo(ms);
    return ms.ToArray();
}}

BlobRequestOptions.ParallelOperationThread所述如下:

Gets or sets the number of blocks that may be simultaneously uploaded.

Remarks:

When using the UploadFrom* methods on a blob, the blob will be broken up into blocks. Setting this value limits the number of outstanding I/O "put block" requests that the library will have in-flight at a given time. Default is 1 (no parallelism). Setting this value higher may result in faster blob uploads, depending on the network between the client and the Azure Storage service. If blobs are small (less than 256 MB), keeping this value equal to 1 is advised.

我假设您可以显式设置 ParallelOperationThreadCount 以加快 blob 上传速度。

var requestOption = new BlobRequestOptions()
{
    ParallelOperationThreadCount = 5 //Gets or sets the number of blocks that may be simultaneously uploaded.
};

//upload a blob from the local file system
await blockBlob.UploadFromFileAsync("{your-file-path}",null,requestOption,null);

//upload a blob from the stream
await blockBlob.UploadFromStreamAsync({stream-for-upload},null,requestOption,null);

foreach (HttpContent ctnt in provider.Contents)

根据您的代码,我假设您按如下方式检索 provider 实例:

MultipartMemoryStreamProvider provider = await request.Content.ReadAsMultipartAsync();

此时,您可以使用以下代码上传新的 blob:

var blobname = ctnt.Headers.ContentDisposition.FileName.Trim('"');
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobname);
//set the content-type for the current blob
blockBlob.Properties.ContentType = ctnt.Headers.ContentType.MediaType;
await blockBlob.UploadFromStreamAsync(await ctnt.Content.ReadAsStreamAsync(), null,requestOption,null);

我更愿意使用 MultipartFormDataStreamProvider which would store the uploaded files from the client to the file system instead of MultipartMemoryStreamProvider which would use the server memory for temporarily storing the data sent from the client. For the MultipartFormDataStreamProvider approach, you could follow this similar . Moreover, I would prefer use the Azure Storage Client Library with my Azure function, you could follow Get started with Azure Blob storage using .NET.

更新:

此外,您可以按照此 tutorial 将大文件分成小块,在客户端上传它们,然后将它们合并回您的服务器端。