0

我正在尝试使用 PutBlockList 方法将电影块上传到 C# 中的 Azure blob。我一直在编写测试代码,问题是当我使用 MD5 来保证数据的完整性并且我故意损坏数据时,导致 MD5 值不同,服务器不会拒绝上传并接受它,而在正确的代码它必须被拒绝。

 var upload = Take.CommitBlocks(shot,takeId,data);
 ....
 blob.Properties.ContentMD5 = md5;
 return Task.Factory.FromAsync(blob.BeginPutBlockList(ids,null,null),blob.EndPutBlockList);

在我的测试方法中,我故意破坏了数据,但系统仍然接受数据。我怎样才能解决这个问题 ?在正确的代码中,我应该收到 Error400,但我什么也没得到。

4

2 回答 2

0

请参阅http://blogs.msdn.com/b/windowsazurestorage/archive/2011/02/18/windows-azure-blob-md5-overview.aspx。Put Block List 不验证 MD5,但 MD5在每个 Put Block 调用上单独验证

于 2012-11-01T19:15:26.120 回答
0

我迟到了几年,但据我所知,这个功能仍然没有内置到 API 和 SDK 中(Assembly Microsoft.WindowsAzure.Storage,Version=8.1.4.0)。也就是说,这是我的工作:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;

/// <summary>
/// Extension methods for <see cref="CloudBlockBlob"/>
/// </summary>
public static class CloudBlockBlobExtensions
{
    /// <summary>
    /// Attempts to open a stream to download a range, and if it fails with <see cref="StorageException"/> 
    /// then the message is compared to a string representation of the expected message if the MD5
    /// property does not match the property sent.
    /// </summary>
    /// <param name="instance">The instance of <see cref="CloudBlockBlob"/></param>
    /// <returns>Returns a false if the calculated MD5 does not match the existing property.</returns>
    /// <exception cref="ArgumentNullException">If <paramref name="instance"/> is null.</exception>
    /// <remarks>This is a hack, and if the message from storage API changes, then this will fail.</remarks>
    public static async Task<bool> IsValidContentMD5(this CloudBlockBlob instance)
    {
        if (instance == null)
            throw new ArgumentNullException(nameof(instance));

        try
        {
            using (var ms = new MemoryStream())
            {
                await instance.DownloadRangeToStreamAsync(ms, null, null);
            }
        }
        catch (StorageException ex)
        {
            return !ex.Message.Equals("Calculated MD5 does not match existing property", StringComparison.Ordinal);
        }

        return true;
    }
}
于 2017-10-12T19:51:18.790 回答