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

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.

主站蜘蛛池模板: 铁力市| 张家港市| 合作市| 旺苍县| 志丹县| 特克斯县| 田林县| 惠水县| 延寿县| 沙雅县| 扎兰屯市| 武威市| 东阳市| 牙克石市| 巴塘县| 德安县| 格尔木市| 武陟县| 江华| 东山县| 贡觉县| 石柱| 海口市| 西丰县| 林周县| 遂溪县| 隆德县| 法库县| 祁门县| 桂林市| 时尚| 普陀区| 临湘市| 宁陵县| 汉川市| 广丰县| 林芝县| 时尚| 右玉县| 曲沃县| 景德镇市|