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

  • 相关阅读:
    Tomcat安装(安装版)
    Selenium自动化测试(一)之环境搭建
    Windows快速启动应用高效搜索文件工具-Listary
    Python3之jsonpath使用和json转换
    Python3操作SQLite数据库
    初识面向对象
    忘记虚拟机中Linux的登录密码解决办法
    win10自带虚拟机的使用(Hyper-v)
    nigx下配置tp5.1路由
    PHP无限极菜单
  • 原文地址:https://www.cnblogs.com/taro/p/6533693.html
Copyright © 2011-2022 走看看