问题表述
直接使用CloudBlockBlob对象获取的Properties是空的,无法获取到对象的MD5值,后台并未进行属性值的填充
前提:blob属性本省包含md5值,某些方式上传的blob默认并没有md5值
解决办法
方法一:使用FetchAttributes()填充blob的属性值和元数据
方法二:使用List方法遍历container中的对象,通过对象的属性获取MD5
Code Demo
方法一:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<storage connection string>");//storage connection string
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("hello");//container name
//Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("tt.txt");
blockBlob.FetchAttributes();//填充blob的属性值和元数据
string md5 = blockBlob.Properties.ContentMD5;
Console.WriteLine("md5:" + md5);
方法二:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<storage connection string>");//storage connection string
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("hello");//container name
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
Console.WriteLine("Block blob of length {0}: {1} + md5:{2}.", blob.Properties.Length, blob.Uri, blob.Properties.ContentMD5);//获取md5值
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
Console.WriteLine("Directory: {0}", directory.Uri);
}
}
相关信息参考链接
通过 .NET 开始使用 Azure Blob 存储
Windows Azure Blob MD5 Overview
Blob Service REST API