简体   繁体   English

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

[英]Azure Storage Calculated MD5 does not match existing property

I'm trying to pass an Azure Storage blob through an ashx.我想通过一个ASHX传递一个Azure存储BLOB。 On the blockBlob.DownloadToStream(memoryStream) it's throwing the following Exception: Microsoft.WindowsAzure.Storage.StorageException: Calculated MD5 does not match existing propertyblockBlob.DownloadToStream(MemoryStream的)它抛出以下异常: Microsoft.WindowsAzure.Storage.StorageException: Calculated MD5 does not match existing property

I know it's finding the correct blob.我知道这是找到正确的斑点。 If I put in a container and path that don't exist then it gives me a 404 exception instead.如果我把一个容器和路径不存在,那么它给了我一个404异常,而不是。

I've Googled for hints on what might be causing this error but nothing useful is coming up.我GOOGLE上什么可能导致这个错误,但没有什么用处快到了提示。 Does anyone have any thoughts on what might be causing this?有没有人对可能导致这种情况的原因有任何想法? I've rewritten this code a couple different ways over the last couple days but it always dies on DownloadToStream.我已经重写这段代码一对夫妇不同的方式,在过去几天,但它总是死在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();
    }
}

I was able to recreate the problem you're facing.我能够重现您所遇到的问题。 This happens if the Content MD5 property of the blob is somehow corrupted.如果发生这种情况的Content MD5斑点的财产以某种方式损坏。 I had a blob with some content MD5 (which was correct).我曾与一些内容,MD5(这是正确的)斑点。 I then programmatically changed the MD5 to some other value (which is incorrect).我然后以编程方式改变了MD5为其他值(这是不正确的)。 Now when I call DownloadToStream() method on the blob, I get exact same error.现在,当我打电话DownloadToStream的BLOB()方法,我得到确切同样的错误。

You can bypass this check by setting DisableContentMD5Validation to true in BlobRequestOptions as shown in the code below:您可以通过设置绕过这个检查DisableContentMD5ValidationtrueBlobRequestOptions如下图所示的代码:

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

Give it a try and it should work.给它一个尝试,它应该工作。

On a side note, you may want to modify your ReadFully method as well.在一个侧面说明,你可能需要修改你ReadFully方法为好。 You would need to move the input stream pointer to the beginning.您需要在移动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();
        }
    }

I had this problem on my local DEV environment.我在本地 DEV 环境中遇到了这个问题。 And it seems that db of AzureStorageEmulator got corrupted.而且似乎AzureStorageEmulator数据库已损坏。

The solution (for local env!):解决方案(对于本地环境!):

  • drop the emulator's db (eg AzureStorageEmulatorDb57 )删除模拟器的数据库(例如AzureStorageEmulatorDb57
  • run AzureStorageEmulator.exe init -sqlinstance .运行AzureStorageEmulator.exe init -sqlinstance . (you may need to customize the instance name) (您可能需要自定义实例名称)
  • run AzureStorageEmulator.exe start运行AzureStorageEmulator.exe start
  • restart the application, so it gets a new handler to the emulator重新启动应用程序,以便为模拟器获取一个新的处理程序

I had the same issue.我有同样的问题。 I used AzureStorageEmulator.exe init -forcecreate .我使用了AzureStorageEmulator.exe init -forcecreate Described in this link .在此链接中描述。 MD5 error message is now gone. MD5 错误消息现已消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM