如何获取Azure Storage Blob的MD5值

问题表述

直接使用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

转载于:https://www.cnblogs.com/taro/p/6533693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值