Azure存储计算的MD5与现有属性不匹配

我正在尝试通过ashx传递Azure存储blob。 在blockBlob.DownloadToStream(memoryStream)上抛出以下exception: Microsoft.WindowsAzure.Storage.StorageException: Calculated MD5 does not match existing property

我知道它找到了正确的blob。 如果我放入一个不存在的容器和路径,那么它会给我一个404exception。

我已经用Google搜索了可能导致此错误的提示,但没有任何有用的信息。 有没有人对可能导致这种情况的原因有任何想法? 在过去的几天里,我以不同的方式重写了这段代码,但它总是在DownloadToStream上消失。

 using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Auth; using Microsoft.WindowsAzure.Storage.Blob; public void ProcessRequest(HttpContext context) { // Retrieve storage account from connection string. Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("gmt"); // Retrieve reference to blob named "articles/142/222.jpg". CloudBlockBlob blockBlob = container.GetBlockBlobReference("articles/142/222.jpg"); using (var memoryStream = new MemoryStream()) { blockBlob.DownloadToStream(memoryStream); byte[] photoByte = ReadFully(memoryStream); context.Response.Clear(); context.Response.ContentType = "image/jpeg"; context.Response.OutputStream.Write(photoByte, 0, photoByte.Length); } } public static byte[] ReadFully(Stream input) { input.Position = 0; using (MemoryStream ms = new MemoryStream()) { input.CopyTo(ms); return ms.ToArray(); } } 

我能够重现你所面临的问题。 如果blob的Content MD5属性以某种方式损坏,则会发生这种情况。 我有一个含有一些内容MD5的blob(这是正确的)。 然后我以编程方式将MD5更改为其他值(这是不正确的)。 现在,当我在blob上调用DownloadToStream()方法时,我得到完全相同的错误。

您可以通过在BlobRequestOptions DisableContentMD5Validation设置为true来绕过此检查,如下面的代码所示:

  BlobRequestOptions options = new BlobRequestOptions() { DisableContentMD5Validation = true, }; blockBlob.DownloadToStream(memoryStream, null, options); 

尝试一下,它应该工作。

另外,您可能还想修改ReadFully方法。 您需要将input流指针移动到开头。

  public static byte[] ReadFully(Stream input) { input.Position = 0;//Positioning it to the top of stream. using (MemoryStream ms = new MemoryStream()) { input.CopyTo(ms); return ms.ToArray(); } }