15分钟后Azure上传Blob失败

时间:2019-07-10 15:03:29

标签: c# upload azure-storage azure-blob-storage

我正在使用DataMovement库将文件上传到Azure存储帐户。 Blob大小为96MB。

如果15分钟后每次连接速度都很慢,则上传失败,并显示以下错误: 发生一个或多个错误。 (传输失败。)

我的代码:

CloudBlockBlob blockBlob = new CloudBlockBlob(new Uri(sConnString));
ServicePointManager.Expect100Continue = false;
TransferManager.Configurations.ParallelOperations = 10;
...
var task = TransferManager.UploadAsync(pathFile, blockBlob, null, context, CancellationToken.None);                
task.Wait();

我该如何解决?为什么要15分钟?

错误StackTrace:

   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at TeamSystem.Enterprise.Cloud.Migrator.Engine.CloudServices.UploadFile(String pathFile, String blobName, Boolean testMode) in C:\EnterpriseCloud\Migrator\TeamSystem.Enterprise.Cloud.Migrator.Engine\Code\CloudServices.cs:line 86

非常感谢您。

2 个答案:

答案 0 :(得分:0)

我尝试添加重试逻辑,但是似乎不起作用。

在catch异常中,我已添加了对恢复交易的方法的调用:

...
catch (Exception e) {
ResumeTransfert(context, pathFile, blockBlob);
}

private void ResumeTransfert(SingleTransferContext context, string pathFile, CloudBlockBlob blockBlob)
{
    SingleTransferContext retryContext = context;
    SingleTransferContext resumeContext = null;

    while (true)
    {
        try
        {
            // Store the transfer checkpoint
            TransferCheckpoint checkpoint = retryContext.LastCheckpoint;                    

            // Create a new TransferContext with the store checkpoint
            resumeContext = new SingleTransferContext(checkpoint);
            resumeContext.ProgressHandler = new Progress<TransferStatus>((progress) => {                    
                if (progress.BytesTransferred > 0)
                {
                    double byteTransferred = Math.Truncate(ByteSize.FromBytes(progress.BytesTransferred).MegaBytes);
                    Log.WriteLog(String.Format("Uploaded: {0} MB", byteTransferred));                            
                }                    
            });

            // Record the overall progress
            ProgressRecorder recorder = new ProgressRecorder();
            resumeContext.ProgressHandler = recorder;

            // Resume transfer from the stored checkpoint
            Console.WriteLine("Resume the cancelled transfer.");
            var task = TransferManager.UploadAsync(pathFile, blockBlob, null, resumeContext);
            task.Wait();

            return;
        }
        catch (Exception e)
        {
            Log.WriteLog("ResumeTransfert: " + e.Message, LogType.Error, LogLevel.Standard);
            retryContext = resumeContext;

            continue;
        }
    }          
}

每次我捕获异常并使用最后一个检查点重新上传,但是它不起作用。 我哪里错了?

谢谢。

答案 1 :(得分:0)

我找到了解决方案!问题出在“ TransferManager.Configurations.ParallelOperations”属性中。

在连接速度慢的情况下,尝试将该值从64减小到2,我没有问题。

所以我创建了一个这样的函数来从连接速度上刺激“ ParallelOperations”:

private int GetParallelOperations(double transfertSpeed)
        {
            int retval = 0;
            switch (transfertSpeed)
            {
                case double n when (n >= 1):
                    retval = Environment.ProcessorCount * 8;
                    break;
                case double n when (n < 1 && n>=0.1):
                    retval = 10;
                    break;                
                default:
                    retval = 2;
                    break;
            }

            return retval;
        }

然后:

TransferManager.Configurations.ParallelOperations = GetParallelOperations(Utilities.TransferSpeed);

使用大小为5MB的简单文件可以提高传输速度。为了进行速度测试,我将“ ParallelOperations”设置为“ Environment.ProcessorCount * 8”(在连接缓慢的情况下也可以使用)。