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 を検証しませんが、MD5Put 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 に答える