我将位图图像上传到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 https://github.com/zgramana/IOSAzureBlobUploader/tree/master/lib引用了此UploadFromStream,它是根据此解决方案构建的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。 ---&GT; System.Net.ProtocolViolationException:要的字节数 write大于指定的ContentLength。在 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流,System.IO.Stream toStream,Nullable
1 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 [零式] (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政策, Microsoft.WindowsAzure.Storage.OperationContext operationContext) [0x00000] in:0 at 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] in:0 at Client.Mobile.Droid.Source.Azure.AmsHelper + d__6.MoveNext ()[0x0021b]在c:\ testsolution \ AmsHelper.cs:103请求 InformationRequestID:RequestDate:StatusMessage:}
我检查了存储容器,确实上传了正确的流,我可以看到图像并且一切正常,但出于某种原因我得到了这个奇怪的错误。
非常感谢任何修复错误的帮助。我可以根据要求提供任何信息。
答案 0 :(得分:0)
我仍然无法弄清楚错误发生的原因。可能是因为我使用一年前过时的分叉库来上传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;
}