zoukankan      html  css  js  c++  java
  • 如何获取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

  • 相关阅读:
    50多条mysql数据库优化建议
    反向代理|与正向代理区别区别
    CDN技术
    mysql存储过程对900w数据进行操作测试
    Navicat For Mysql快捷键
    Linux下目标文件分析
    Linux下的两个经典宏定义 转
    debuginfo-install glibc-2.17-157.el7.x86_64
    index merge 引起的死锁分析
    linux内核源码中常见宏定义
  • 原文地址:https://www.cnblogs.com/taro/p/6533693.html
Copyright © 2011-2022 走看看