Ejemplo n.º 1
0
        /// <summary>
        /// Apply a content hash to the blob to verify upload and roundtrip consistency.
        /// </summary>
        /// <param name="blob">Cloud Blob</param>
        /// <param name="stream">Stream value</param>
        public void ApplyContentMd5Hash(ICloudBlob blob, Stream stream)
        {
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var hash = _cryptographyHelper.ComputeStreamMd5Hash(stream);

            blob.FetchAttributesAsync(null, _blobRequestOptionsHelper.Get(), null).Wait();

            // StorageClient does not provide a way to retrieve MD5 so we add our own MD5 check
            // which let perform our own validation when downloading the blob
            if (blob.Metadata.ContainsKey(METADATA_MD5_KEY))
            {
                blob.Metadata[METADATA_MD5_KEY] = hash;
            }
            else
            {
                blob.Metadata.Add(new KeyValuePair <string, string>(METADATA_MD5_KEY, hash));
            }

            blob.SetMetadataAsync().Wait();
        }