azure - 如何处理附加 blob 异常?

标签 azure azure-storage azure-blob-storage

如何正确处理“提交的 block 数不能超过 50,000 block 的最大限制。”执行 CloudAppendBlob.AppendTextAsync 时出现异常?

我目前有以下代码:

    private async Task MessageLogger()
    {
        string messages = "";
        while (true)
        {
            // Check if a new log file should be created
            if (DateTime.UtcNow.Day != _logCreatedUtc.Day)
            {
                // Create a new log file
                await CreateNewLogAsync();
            }

            if (_messageQueue.Count == 0 && messages.Length == 0)
            {
                await Task.Delay(50);
                continue;
            }

            int n = 0;
            while ((this._messageQueue.Count > 0) && (n < 50))
            {
                string message;
                if (this._messageQueue.TryPeek(out message))
                {
                    messages += message;
                    this._messageQueue.TryDequeue(out message);
                    n++;
                }
            }

            try
            {
                // Append messages to Azure Blob
                await _appendBlob.AppendTextAsync(messages);
                messages = "";
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException exception)
                when(exception.RequestInformation.HttpStatusCode == 404)
            {
                // Log file was deleted. Create a new log file
                await CreateNewLogAsync();
            }
            catch (Exception exception)
            {
                LogException("Exception from CloudAppendBlob.AppendTextAsync", exception);
                await Task.Delay(1000);
            }
        }
    }

有时,由于代码中的通用异常处理程序,我会记录以下类型的异常:

01.04.2018 22:17:03.030 - <<EXCEPTION>>: Exception from CloudAppendBlob.AppendTextAsync
01.04.2018 22:17:03.044 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The committed block count cannot exceed the maximum limit of 50,000 blocks.))> InnerException.Message=<One or more errors occurred. (The committed block count cannot exceed the maximum limit of 50,000 blocks.)>
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183

异常处理程序如何捕获此类错误?我想为其添加一个特定的处理程序。

我尝试在调试器中查看,但它看起来很奇怪:

enter image description here

异常变量在调试器中看起来像是“null”,但实际上它不是,并且记录正常。

最佳答案

我咨询了产品团队,他们建议实现并使用 AccessConditions 进行 block 追加以避免异常发生:

 /// <summary>
    /// Gets or sets a value for a condition that specifies the maximum size allowed for an append blob when a new block is committed. The append
    /// will succeed only if the size of the blob after the append operation is less than or equal to the specified size.
    /// </summary>
    /// <value>The maximum size in bytes, or <c>null</c> if no value is set.</value>
    /// <remarks>This condition only applies to append blobs.</remarks>
    public long? IfMaxSizeLessThanOrEqual

本质上,您将检查 block 的数量,并根据条件,您可以启动一个新的 blob 来附加(如果达到最大值),或继续附加。

这两个会有帮助:

x-ms-blob-condition-maxsize: Clients can use this header to ensure that append operations do not increase the blob size beyond an expected maximum size in bytes. If the condition fails, the request will fail with MaxBlobSizeConditionNotMet error (HTTP status code 412 – Precondition Failed).

x-ms-blob-condition-appendpos Optional conditional header, used only for the Append Block operation. A number indicating the byte offset to compare. Append Blockwill succeed only if the append position is equal to this number. If it is not, the request will fail with the AppendPositionConditionNotMet error (HTTP status code 412 – Precondition Failed).

来源:** https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/AccessCondition.cs **

https://learn.microsoft.com/en-us/rest/api/storageservices/append-block

关于azure - 如何处理附加 blob 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49627812/

相关文章:

windows - 将文件上传到 Azure 存储后如何获取文件

azure - 无法在 Windows Phone 8 应用程序中安装 Windows Azure SDK

rest - Azure REST WebClient PUT Blob

angularjs - 如何使用 cordova api 将照片上传到 azure blob 存储?

c# - 调用 Dispatch/LUIS 模型时操作返回无效状态代码 ‘Unauthorized’

azure - 从逻辑应用连接到 Azure 函数槽

azure - 从 Azure 流式传输视频(Blob 或媒体服务)

c# - 使用 .net 从 Iothub 配置获取部署 list

Azure 函数 function.json 生成的 scriptFile 路径不正确

javascript - for await 给出 SyntaxError : Unexpected reserved word inside a async function