【问题标题】:Uploading to blob by PutBlockList method in C# and MD5 check通过 C# 中的 PutBlockList 方法上传到 blob 和 MD5 检查
【发布时间】:2012-10-22 09:55:48
【问题描述】:

我正在尝试使用 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,但我什么也没得到。

【问题讨论】:

    标签: c# azure upload


    【解决方案1】:

    我迟到了几年,但据我所知,此功能仍未内置到 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-01-31
        • 2016-05-20
        • 2016-04-27
        • 1970-01-01
        • 2015-10-16
        • 2016-07-04
        • 2020-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多