块文件流并通过asp.net core将它们发送到azure blob

时间:2018-04-05 10:27:01

标签: c# azure asp.net-core

当我尝试将大文件(256mb~)发送到azure blob时,有人可以帮我解决问题。错误是OutofMemoryException。应用程序托管在webApp上的azure与3.5gb ram计划。 转移模式如下: - 从这里发送文件

<form method="post" enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Post" style="margin-top: 100px;">
<div>
    <p>Upload one or more files using this form:</p>
    <input type="file" name="files" multiple />
</div>
<div>
    <p>Your Username</p>
    <input type="text" name="username" />
</div>
<div>
    <input type="submit" value="Upload" />
</div>

控制器:

public async Task<IActionResult> Post()
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("");
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("testcontainer");
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    FormValueProvider formModel;

    formModel = await Request.StreamFile(storageAccount);

-stream文件扩展我从microsoftdocs中的示例中获取,在其中我将块内存流放在blockblob上并将它们作为BlockList发送

var formAccumulator = new KeyValueAccumulator();
string targetFilePath = null;

var boundary = MultipartRequestHelper.GetBoundary(
    MediaTypeHeaderValue.Parse(request.ContentType),
    DefaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, request.Body);

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

    if (hasContentDispositionHeader)
    {
        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
        {
            using (var stream = new MemoryStream())
            {
                await section.Body.CopyToAsync(stream);
                stream.Position = 0;

                await new AzureBlobUtil().UploadBlob(stream, blockBlob, "testcontainer", "testBlob");
            }

我认为问题出现在内存流中,导致在发送到azure之前存储整个文件,但我不知道如何将其分块。我的意思是idk如何发送部分MultipartReader。

0 个答案:

没有答案