官术网_书友最值得收藏!

Setting properties and metadata for a blob

The Windows Azure Blob Service allows metadata and properties to be associated with a blob. The metadata comprises a sequence of developer-defined, name-value pairs. The properties comprise HTTP request headers including: Cache-Control, Content-Encoding, Content-MD5, and Content-Type. The Blob service also supports metadata and properties for blob containers. Requesting a download of the blob attributes—its metadata and properties—is an efficient way of checking blob existence as only a small amount of data is downloaded.

The Blob service uses the Content-MD5 property to validate blob upload. When specified, the Content-MD5 property must be set to the base64-encoded value of the MD5 hash of the blob data. The Blob service returns an error if this value does not match the value it calculates from the uploaded blob content.

In this recipe, we will learn how to set and get blob properties and metadata. We will also learn how to calculate the Content-MD5 property.

How to do it...

We are going to create a blob and add some metadata and the Content-MD5 property to it. We will then fetch the attributes of the blob to retrieve the property and metadata. We do this as follows:

  1. Add a new class named PropertiesMetadataExample to the project.
  2. Set the Target Framework for the project to .NET Framework 4.
  3. Add the following assembly references to the project:
    Microsoft.WindowsAzure.StorageClient
    System.Configuration
  4. Add the following using statements to the top of the class file:
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using System.Configuration;
    using System.Security.Cryptography;
    using System.Collections.Specialized;
  5. Add the following member to the class:
    CloudBlobClient cloudBlobClient;
  6. Add the following constructor to the class:
    public PropertiesMetadataExample()
    {
      CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
      cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient();
    }
  7. Add the following method, uploading a blob as a Byte array, to the class:
    private void UploadByteArray(String containerName, String blobName)
    {
      CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName);
      cloudBlobContainer.CreateIfNotExist();
    
      Byte[] blobData = { 0x41, 0x7A, 0x75, 0x72, 0x65 };
      CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName);
      cloudBlockBlob.Metadata["TodayIs"] = "Wednesday";
      cloudBlockBlob.Attributes.Properties.ContentMD5 =CalculateMD5Hash(blobData);
      cloudBlockBlob.UploadByteArray(blobData);
    }
  8. Add the following method, retrieving blob attributes, to the class:
    private void FetchAttributes(String containerName,String blobName)
    {
      CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName);
      CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName);
    
      cloudBlockBlob.FetchAttributes();
    
      BlobProperties blobProperties =cloudBlockBlob.Attributes.Properties;
      NameValueCollection blobMetadata =cloudBlockBlob.Attributes.Metadata;
      foreach (String key in blobMetadata)
      {
        String value = blobMetadata[key];
      }
    }
  9. Add a method, calculating the MD5 hash, to the class:
    private static String CalculateMD5Hash(Byte[] bytes)
    {
      MD5 md5 = MD5.Create();
      Byte[] md5Hash = md5.ComputeHash(bytes);
      String base64EncodedMD5Hash =Convert.ToBase64String(md5Hash);
    
      return base64EncodedMD5Hash;
    }
  10. Add the following method, using the methods added earlier, to the class:
    public static void UsePropertiesMetadataExample()
    {
      String containerName = "{CONTAINER_NAME}";
      String blobName = "{BLOB_NAME}";
    
      PropertiesMetadataExample example = newPropertiesMetadataExample();
      example.UploadByteArray(containerName, blobName);
      example.FetchAttributes(containerName, blobName);
    }
  11. Add the following to the configuration section of app.config:
    <appSettings>
      <add key="DataConnectionString" value="DefaultEndpointsProtocol=http;AccountName={ACCOUNT_NAME};AccountKey={ACCOUNT_KEY}"/> 
    </appSettings>

How it works...

In steps 1 through 4, we set up the class. In step 5, we add a member to hold the CloudBlobClient instance we initialize in the constructor we add in step 6. We initialize the CloudStorageAccount instance from app.config.

In step 7, we add a method that creates a blob out of a Byte array, adds the calculated Content-MD5 property to the blob, and associates some metadata, named TodayIs , with the blob. In step 8, we add a method that retrieves the attributes of a blob and iterates over any metadata associated with it.

In step 9, we add the method that calculates the base64-encoded MD5 hash for a Byte[].

In step 10, we add a method that invokes the methods we added to the class. We must replace {CONTAINER_NAME} and {BLOB_NAME} with appropriate container and blob names.

In step 11, we add the connection string to the app.config configuration file. We must replace {ACCOUNT_NAME} and {ACCOUNT_KEY} with actual values for account name and access key.

主站蜘蛛池模板: 廊坊市| 遵义县| 玉树县| 兴宁市| 台南县| 临海市| 边坝县| 天水市| 泰州市| 荆州市| 佛冈县| 丁青县| 青川县| 康马县| 墨脱县| 乐东| 江城| 工布江达县| 隆尧县| 贺州市| 遂昌县| 印江| 石屏县| 高唐县| 兴隆县| 徐闻县| 东平县| 鹰潭市| 历史| 富川| 和田县| 扶沟县| 垦利县| 读书| 黄冈市| 苏尼特右旗| 铜山县| 安宁市| 西峡县| 博湖县| 勃利县|