问题
I'm uploading bitmap images to Azure Blob Storage. It was all working fine, but then I made a new project to use the same code but now I get this error consistently "The number of bytes to be written is great than the specified ContentLength"
Here's part of my code 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;
}
The problem is in the UploadFromStream function, I don't know why it doesn't work anymore.
This UploadFromStream is referenced in this dll https://github.com/zgramana/IOSAzureBlobUploader/tree/master/lib which is built from this solution https://github.com/zgramana/azure-sdk-for-net
Here's the UploadFromStream() function which is referenced from the dll
/// <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 */);
}
}
}
I've been doing hours of research trying to figure out how to fix this error but couldn't. I am NOT explicitly setting the ContentLength value in anyway (matter of fact I don't even have access to it since UploadFromStream does it by itself I believe)
The capacity of memorystream is 2048 bytes, it's length is 1415 bytes
Here's the stack trace of the error
{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, 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[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:}
I've checked the storage container, and the stream does indeed get uploaded correctly, and I can see the image and all fine, but for some reason I get this strange error.
Any help fixing the error would be much appreciated. I can provide any info upon request.
回答1:
I still couldn't figure out why the error occurs. Probably cause I'm using a one-year-old outdated forked library to upload blobs, that library wasn't even meant to support the platform I'm using from the first place.
Anyway, turns out the blobs are uploaded pretty much fine, so all I needrf to do was to suppress that error
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;
}
来源:https://stackoverflow.com/questions/23987351/strange-sudden-error-the-number-of-bytes-to-be-written-is-greater-than-the-spec