gpt4 book ai didi

c# - 奇怪的突然错误 "The number of bytes to be written is greater than the specified ContentLength"

转载 作者:行者123 更新时间:2023-12-03 03:16:49 25 4
gpt4 key购买 nike

我正在将位图图像上传到 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函数上,我不知道为什么它不再起作用了。

此 dll 中引用了此 UploadFromStream 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 是自己完成的)

内存流的容量为2048字节,长度为1415字节

这是错误的堆栈跟踪

{Microsoft.WindowsAzure.Storage.StorageException: The number of bytes to be written is greater than the specified ContentLength. ---> System.Net.ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength. at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in :0 at System.Net.WebConnectionStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in :0 at System.Net.WebConnectionStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in :0 at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync (System.IO.Stream stream, System.IO.Stream toStream, Nullable1
maxLength, Nullable
1 expiryTime, Boolean calculateMd5, Boolean syncRead, Microsoft.WindowsAzure.Storage.OperationContext 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.StorageCommandBase
1 cmd, IRetryPolicy policy, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] in :0 at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream (System.IO.Stream source, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] in :0 at Client.Mobile.Droid.Source.Azure.AmsHelper+d__6.MoveNext () [0x0021b] in c:\testsolution\AmsHelper.cs:103 Request InformationRequestID:RequestDate:StatusMessage:}

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

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

最佳答案

我还是不明白为什么会出现这个错误。可能是因为我使用一个已有一年历史的过时的 fork 库来上传 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;
}

关于c# - 奇怪的突然错误 "The number of bytes to be written is greater than the specified ContentLength",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com