大文件上传到Azure文件存储失败 [英] Uploading to Azure File Storage fails with large files

查看:86
本文介绍了大文件上传到Azure文件存储失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试上传大于4MB的文件会导致引发 RequestBodyTooLarge 异常,并显示以下消息:

Attempting to upload a file larger than 4MB results in a RequestBodyTooLarge exception being thrown with the following message:

The request body is too large and exceeds the maximum permissible limit.

REST API参考中记录了此限制(https://docs.microsoft.com/zh-cn/rest/api/storageservices/put-range ),但未针对SDK Upload *方法进行记录(

While this limit is documenting in the REST API reference (https://docs.microsoft.com/en-us/rest/api/storageservices/put-range) it is not documented for the SDK Upload* methods (https://docs.microsoft.com/en-us/dotnet/api/azure.storage.files.shares.sharefileclient.uploadasync?view=azure-dotnet). There are also no examples of working around this.

那么如何上传大文件?

推荐答案

经过反复试验,我能够创建以下方法来解决文件上传限制.在 _dirClient 下面的代码中,一个已经初始化的 ShareDirectoryClient 设置为我要上传到的文件夹.

After much trial and error I was able to create the following method to work around the file upload limits. In the code below _dirClient is an already initialized ShareDirectoryClient set to the folder I'm uploading to.

如果传入的流大于4MB,则代码将从中读取4MB的块并将其上传直到完成. HttpRange 是将字节添加到已经上传到Azure的文件中的位置.索引必须增加以指向Azure文件的末尾,因此将附加新的字节.

If the incoming stream is larger than 4MB the code reads 4MB chunks from it and uploads them until done. The HttpRange is where the bytes will be added to the file already uploaded to Azure. The index has to be incremented to point to the end of the Azure file so the new bytes will be appended.

public async Task WriteFileAsync(string filename, Stream stream) {

    //  Azure allows for 4MB max uploads  (4 x 1024 x 1024 = 4194304)
    const int uploadLimit = 4194304;

    stream.Seek(0, SeekOrigin.Begin);   // ensure stream is at the beginning
    var fileClient = await _dirClient.CreateFileAsync(filename, stream.Length);

    // If stream is below the limit upload directly
    if (stream.Length <= uploadLimit) {
        await fileClient.Value.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
        return;
    }

    int bytesRead;
    long index = 0;
    byte[] buffer = new byte[uploadLimit];

    // Stream is larger than the limit so we need to upload in chunks
    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) {
        // Create a memory stream for the buffer to upload
        using MemoryStream ms = new MemoryStream(buffer, 0, bytesRead);
        await fileClient.Value.UploadRangeAsync(new HttpRange(index, ms.Length), ms);
        index += ms.Length; // increment the index to the account for bytes already written
    }
}

这篇关于大文件上传到Azure文件存储失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆