【发布时间】:2020-05-10 06:36:46
【问题描述】:
通过 Postman 上传大文件时(从前端使用 php 编写的表单,我遇到了同样的问题)我从 Azure Web 应用程序收到 502 bad gateway 错误消息:
502 - Web 服务器在充当 网关或代理服务器。你所在的页面有问题 正在寻找,无法显示。当 Web 服务器(而 作为网关或代理)联系上游内容服务器, 它收到了来自内容服务器的无效响应。
我在 Azure 应用程序洞察中看到的错误:
Microsoft.AspNetCore.Connections.ConnectionResetException:客户端 已断开连接
尝试上传 2GB 测试文件时会发生这种情况。对于 1GB 的文件,它可以正常工作,但需要达到 ~5GB。
我已经优化了将文件流写入天蓝色 blob 存储的部分,方法是使用块写入方法(归功于:https://www.red-gate.com/simple-talk/cloud/platform-as-a-service/azure-blob-storage-part-4-uploading-large-blobs/),但对我来说,似乎连接正在关闭客户端(邮递员在这种情况下),因为这似乎是单个 HTTP POST 请求,并且底层 Azure 网络堆栈(例如负载均衡器)正在关闭连接,因为我的 API 为 HTTP POST 请求提供返回 HTTP 200 OK 需要很长时间。
我的假设正确吗?如果是,如何实现从我的前端(或邮递员)上传以块(例如 15MB)的形式发生,然后 API 可以以比整个 2GB 更快的方式确认?即使创建一个用于上传到 azure blob 并将 URL 返回到浏览器的 SAS URL 也可以,但我不确定如何轻松集成它 - 还有最大块大小 afaik,所以对于 2GB,我可能需要创建多个块。如果这是建议,那么在这里获得一个好的样品会很棒,但也欢迎其他想法!
这是我在 C# .Net Core 2.2 中的 API 控制器端点中的相关部分:
[AllowAnonymous]
[HttpPost("DoPost")]
public async Task<IActionResult> InsertFile([FromForm]List<IFormFile> files, [FromForm]string msgTxt)
{
...
// use generated container name
CloudBlobContainer container = blobClient.GetContainerReference(SqlInsertId);
// create container within blob
if (await container.CreateIfNotExistsAsync())
{
await container.SetPermissionsAsync(
new BlobContainerPermissions
{
// PublicAccess = BlobContainerPublicAccessType.Blob
PublicAccess = BlobContainerPublicAccessType.Off
}
);
}
// loop through all files for upload
foreach (var asset in files)
{
if (asset.Length > 0)
{
// replace invalid chars in filename
CleanFileName = String.Empty;
CleanFileName = Utils.ReplaceInvalidChars(asset.FileName);
// get name and upload file
CloudBlockBlob blockBlob = container.GetBlockBlobReference(CleanFileName);
// START of block write approach
//int blockSize = 256 * 1024; //256 kb
//int blockSize = 4096 * 1024; //4MB
int blockSize = 15360 * 1024; //15MB
using (Stream inputStream = asset.OpenReadStream())
{
long fileSize = inputStream.Length;
//block count is the number of blocks + 1 for the last one
int blockCount = (int)((float)fileSize / (float)blockSize) + 1;
//List of block ids; the blocks will be committed in the order of this list
List<string> blockIDs = new List<string>();
//starting block number - 1
int blockNumber = 0;
try
{
int bytesRead = 0; //number of bytes read so far
long bytesLeft = fileSize; //number of bytes left to read and upload
//do until all of the bytes are uploaded
while (bytesLeft > 0)
{
blockNumber++;
int bytesToRead;
if (bytesLeft >= blockSize)
{
//more than one block left, so put up another whole block
bytesToRead = blockSize;
}
else
{
//less than one block left, read the rest of it
bytesToRead = (int)bytesLeft;
}
//create a blockID from the block number, add it to the block ID list
//the block ID is a base64 string
string blockId =
Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}",
blockNumber.ToString("0000000"))));
blockIDs.Add(blockId);
//set up new buffer with the right size, and read that many bytes into it
byte[] bytes = new byte[bytesToRead];
inputStream.Read(bytes, 0, bytesToRead);
//calculate the MD5 hash of the byte array
string blockHash = Utils.GetMD5HashFromStream(bytes);
//upload the block, provide the hash so Azure can verify it
blockBlob.PutBlock(blockId, new MemoryStream(bytes), blockHash);
//increment/decrement counters
bytesRead += bytesToRead;
bytesLeft -= bytesToRead;
}
//commit the blocks
blockBlob.PutBlockList(blockIDs);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("Exception thrown = {0}", ex);
// return BadRequest(ex.StackTrace);
}
}
// END of block write approach
...
这是一个通过 Postman 的 HTTP POST 示例:
我已经在 web.config 中设置了 maxAllowedContentLength 和 requestTimeout 以进行测试:
requestLimits maxAllowedContentLength="4294967295"
和
aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" requestTimeout="00:59:59" hostingModel="InProcess"
【问题讨论】:
-
除了延长 timeout 和 maxContentLength 或使用
HttpPostedFileBase作为模型,您可以在客户端使用 JavaScript 将文件拆分为chunks。然后发送大量 100 MB 的小块。 -
上传大文件,建议你可以试试Azure Storage Data Movement Library。
-
我认为我认为将 IFormFile 用于大文件是一个坏主意,因为它会将其加载到内存中。
-
@Charles 谢谢。 HttpPostedFileBase 会改变什么吗?有没有在客户端使用 JavaScript 分块发送的示例?
-
gist.github.com/shiawuen/1534477 看看这个,很简单。在服务器端,您只需保存所有部分并将它们合并在一起,它只是 byte[] 数组。
标签: c# azure .net-core azure-blob-storage