Kestrel:服务器已使连接超时

时间:2019-10-30 19:52:39

标签: asp.net-core azure-blob-storage kestrel-http-server

我正在尝试通过Kestrel托管的ASP.net Core应用将非常大的视频文件(8gb +)上传到Azure存储。

我关注了文档here,并做了一些更改以使其与Blob存储一起使用。一切都可以用较小的文件(例如256MB)工作,但是随着文件的增大,我会收到以下错误消息:

System.IO.IOException: The read operation failed, see inner exception. ---> 
Microsoft.AspNetCore.Connections.ConnectionAbortedException:
The connection was timed out by the server.

这是我的代码:

[HttpPost("~/api/uploadMediaAssets")]
[RequestSizeLimit(long.MaxValue)]
[RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
[DisableFormValueModelBindingAttribute]
[Authorize]
public async Task<IActionResult> UploadMediaAsset([FromQuery]long courseId) {

    var _defaultFormOptions = new FormOptions();

    if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) {
        ModelState.AddModelError("File",
            $"The request couldn't be processed (Error 1).");
        // Log error

        return BadRequest(ModelState);
    }


    var boundary = MultipartRequestHelper.GetBoundary(
        MediaTypeHeaderValue.Parse(Request.ContentType),
        _defaultFormOptions.MultipartBoundaryLengthLimit);
    var reader = new MultipartReader(boundary, HttpContext.Request.Body);
    var section = await reader.ReadNextSectionAsync();
    int index = 0;

    while (section != null) {
        var hasContentDispositionHeader =
            ContentDispositionHeaderValue.TryParse(
                section.ContentDisposition, out var contentDisposition);

        if (hasContentDispositionHeader) {
            // This check assumes that there's a file
            // present without form data. If form data
            // is present, this method immediately fails
            // and returns the model error.
            if (!MultipartRequestHelper
                .HasFileContentDisposition(contentDisposition)) {
                ModelState.AddModelError("File",
                    $"The request couldn't be processed (Error 2).");
                // Log error

                return BadRequest(ModelState);
            } else {
                // Don't trust the file name sent by the client. To display
                // the file name, HTML-encode the value.
                var trustedFileNameForDisplay = WebUtility.HtmlEncode(
                        contentDisposition.FileName.Value);
                var trustedFileNameForFileStorage = Path.GetRandomFileName();

                var asset = media.CreateAsset(trustedFileNameForDisplay);
                // Connect to Azure
                CloudBlobClient blobClient = mediaAssetStorageAccount.CloudStorageAccount.CreateCloudBlobClient();


                CloudBlobContainer container = blobClient.GetContainerReference(asset.ContainerName);
                //CloudBlobContainer container = new CloudBlobContainer(new Uri(asset));
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(trustedFileNameForDisplay);

                var streamedFileContent = await FileHelpers.ProcessStreamedFile(
                    section, contentDisposition, ModelState);

                if (!ModelState.IsValid) {
                    return BadRequest(ModelState);
                }

                await blockBlob.UploadFromByteArrayAsync(streamedFileContent, index, streamedFileContent.Length);
                index = index + streamedFileContent.Length;
            }
        }

        // Drain any remaining section body that hasn't been consumed and
        // read the headers for the next section.
        section = await reader.ReadNextSectionAsync();
    }

    return Created("Success", null);

}

0 个答案:

没有答案