TransferManager.UploadAsync花了很长时间

时间:2017-11-05 16:31:42

标签: asp.net-mvc azure .net-core azure-storage-blobs

我想把文件上传到azure的blobstorage中。使用小文件上传工作正常,但是当文件变大(如30mb)时,它变得非常慢。我已经尝试了不同的方法,但没有得到任何更好的结果。

当我在运行webapp本地时,需要花费5分钟上传,并且在azure webapp上我没有得到任何结果(可能是这个过程被杀死了)

所以这是我上传内容的代码:

[HttpPost]
[Route("api/upload")]
[DisableRequestSizeLimit]
public async Task<IActionResult> UploadAsync(CancellationToken cancellationToken)
{
    if (!Request.HasFormContentType)
        return BadRequest();
    var form = Request.Form;
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_configuration["CloudStorage:Account"]);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("files");
    for (int i = 0; i < form.Files.Count(); i++)
    {
        using (var readStream = form.Files[i].OpenReadStream())
        {
            String blobname = DateTime.Now.ToString("yyyyMMddHHmmss");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobname);
            TransferManager.Configurations.ParallelOperations = 64;
            SingleTransferContext context = new SingleTransferContext
            {
                ProgressHandler =
                new Progress<TransferStatus>(
                progress => { Console.WriteLine("{1} Bytes uploaded: {0}", progress.BytesTransferred, DateTime.Now.ToString("HH:mm:ss")); })
            };

            // I tryed this 2 options:
            TransferManager.UploadAsync(readStream, blockBlob, null, context, CancellationToken.None).Wait();

            //and:
            using (var fileStream = System.IO.File.Create(System.IO.Path.GetTempPath() + blobname))
            {
                readStream.Seek(0, SeekOrigin.Begin);
                readStream.CopyTo(fileStream);
            }
            TransferManager.UploadAsync(System.IO.Path.GetTempPath() + blobname, blockBlob, null, context, CancellationToken.None).Wait();
            System.IO.File.Delete(System.IO.Path.GetTempPath() + blobname);
        }
    }
    return OK();
}

这是我的控制台输出:

14:03:34 Bytes uploaded: 0
14:03:34 Bytes uploaded: 0
14:03:34 Bytes uploaded: 0
14:03:34 Bytes uploaded: 0
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0\System.Diagnostics.StackTrace.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.    
The thread 13984 has exited with code 0 (0x0).
The thread 5220 has exited with code 0 (0x0).
The thread 9968 has exited with code 0 (0x0).
The thread 10744 has exited with code 0 (0x0).
The thread 13488 has exited with code 0 (0x0).
The thread 9460 has exited with code 0 (0x0).
14:05:54 Bytes uploaded: 1540505
The thread 792 has exited with code 0 (0x0).
14:07:36 Bytes uploaded: 5734809
14:07:36 Bytes uploaded: 9929113
14:07:38 Bytes uploaded: 14123417
14:07:43 Bytes uploaded: 18317721
14:07:53 Bytes uploaded: 22512025
14:07:54 Bytes uploaded: 26706329
14:08:10 Bytes uploaded: 30900633
14:08:13 Bytes uploaded: 35094937
14:08:14 Bytes uploaded: 35094937

所以有人知道问题是什么吗?

0 个答案:

没有答案