【问题标题】:Strange Sudden Error "The number of bytes to be written is greater than the specified ContentLength"奇怪的突然错误“要写入的字节数大于指定的ContentLength”
【发布时间】:2015-03-30 19:47:43
【问题描述】:

我正在将位图图像上传到 Azure Blob 存储。一切正常,但后来我做了一个新项目来使用相同的代码,但现在我一直收到这个错误 "要写入的字节数大于指定的ContentLength"

这是我的部分代码 memoryStream.Seek(0, SeekOrigin.Begin);

try
{
    cloudBlobContainer.GetBlockBlobReference(resourcename).UploadFromStream(memoryStream);

    return true;
}
catch (Exception exception)
{
    ReportHelper.Report(Tag, "Error occurred while uploading image " + imageQuality, true, exception, true);

    return null;
}

问题出在 UploadFromStream 函数中,我不知道为什么它不再起作用了。

这个 UploadFromStream 在这个 dll https://github.com/zgramana/IOSAzureBlobUploader/tree/master/lib 中被引用,它是从这个解决方案 https://github.com/zgramana/azure-sdk-for-net 构建的

这是从 dll 引用的 UploadFromStream() 函数

/// <summary>
/// Uploads a stream to a block blob. 
/// </summary>
/// <param name="source">The stream providing the blob content.</param>
/// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param>
/// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param>
/// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
[DoesServiceRequest]
public void UploadFromStream(Stream source, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
{
    CommonUtils.AssertNotNull("source", source);
    this.attributes.AssertNoSnapshot();
    BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.BlockBlob, this.ServiceClient);
    operationContext = operationContext ?? new OperationContext();

    DateTime? expiryTime = modifiedOptions.MaximumExecutionTime.HasValue
        ? DateTime.Now + modifiedOptions.MaximumExecutionTime.Value
        : (DateTime?)null;

    if ((this.ServiceClient.ParallelOperationThreadCount == 1) &&
        source.CanSeek &&
        ((source.Length - source.Position) <= this.ServiceClient.SingleBlobUploadThresholdInBytes))
    {
        string contentMD5 = null;
        if (modifiedOptions.StoreBlobContentMD5.Value)
        {
            OperationContext tempOperationContext = new OperationContext();
            StreamDescriptor streamCopyState = new StreamDescriptor();
            long startPosition = source.Position;
            source.WriteToSync(Stream.Null, null /* maxLength */, expiryTime, true, true, tempOperationContext, streamCopyState);
            source.Position = startPosition;
            contentMD5 = streamCopyState.Md5;
        }

        Executor.ExecuteSync(
            this.PutBlobImpl(source, contentMD5, accessCondition, modifiedOptions),
            modifiedOptions.RetryPolicy,
            operationContext);
    }
    else
    {
        using (Stream blobStream = this.OpenWrite(accessCondition, modifiedOptions, operationContext))
        {
            source.WriteToSync(blobStream, null /* maxLength */, expiryTime, false, true, new OperationContext(), null /* streamCopyState */);
        }
    }
}

我一直在进行数小时的研究,试图找出如何解决此错误,但无法解决。无论如何我都没有明确设置 ContentLength 值(事实上我什至无法访问它,因为我相信 UploadFromStream 自己会这样做)

memorystream的容量为2048字节,长度为1415字节

这是错误的堆栈跟踪

{Microsoft.WindowsAzure.Storage.StorageException:字节数 要写入的内容大于指定的 ContentLength。 ---> System.Net.ProtocolViolationException:字节数 写入大于指定的 ContentLength。在 System.Net.WebConnectionStream.CheckWriteOverflow(Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in :0 at System.Net.WebConnectionStream.BeginWrite (System.Byte[] 缓冲区,Int32 偏移量,Int32 大小,System.AsyncCallback cb,System.Object 状态)[0x00000] in :0 at System.Net.WebConnectionStream.Write(System.Byte[] 缓冲区,Int32 偏移量,Int32 大小)[0x00000] in :0 at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync (System.IO.Stream 流,System.IO.Stream toStream,Nullable1 maxLength, Nullable1 expiryTime,Boolean calculateMd5,Boolean 同步读取,Microsoft.WindowsAzure.Storage.OperationContext 操作上下文, Microsoft.WindowsAzure.Storage.Core.Util.StreamDescriptor streamCopyState) [0x00000] in :0 at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[NullType] (Microsoft.WindowsAzure.Storage.Core.Executor.StorageCommandBase1 cmd, IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[NullType] (Microsoft.WindowsAzure.Storage.Core.Executor.StorageCommandBase1 cmd, IRetryPolicy 策略, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] 在 :0 处 Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream (System.IO.Stream 源, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions 选项, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] 在 :0 处 Client.Mobile.Droid.Source.Azure.AmsHelper+d__6.MoveNext () [0x0021b] 在 c:\testsolution\AmsHelper.cs:103 请求 InformationRequestID:RequestDate:StatusMessage:}

我检查了存储容器,流确实正确上传,我可以看到图像并且一切正常,但由于某种原因,我收到了这个奇怪的错误。

任何帮助修复错误将不胜感激。我可以根据要求提供任何信息。

【问题讨论】:

    标签: c# .net azure stream memorystream


    【解决方案1】:

    我仍然无法弄清楚错误发生的原因。可能是因为我使用了一个使用了一年的过时的分叉库来上传 blob,该库甚至不打算支持我从一开始就使用的平台。

    无论如何,事实证明 blob 上传得非常好,所以我需要做的就是抑制该错误

    try
    {
        cloudBlobContainer.GetBlockBlobReference(resourcename).UploadFromStream(memoryStream);
    
        return true;
    }
    catch (Microsoft.WindowsAzure.Storage.StorageException storageException)
    {
        return true;
    }
    catch (System.Net.ProtocolViolationException protocolViolationException)
    {
        return true;
    }
    catch (Exception exception)
    {
        ReportHelper.Report(Tag, "Error occurred while uploading image " + imageQuality, true, exception, true);
    
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多