- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 608字
- 2021-04-02 18:51:57
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:
- Add a new class named
PropertiesMetadataExample
to the project. - Set the Target Framework for the project to .NET Framework 4.
- Add the following assembly references to the project:
Microsoft.WindowsAzure.StorageClient System.Configuration
- 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;
- Add the following member to the class:
CloudBlobClient cloudBlobClient;
- Add the following constructor to the class:
public PropertiesMetadataExample() { CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]); cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient(); }
- 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); }
- 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]; } }
- 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; }
- 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); }
- Add the following to the
configuration
section ofapp.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.
- ModelSim電子系統(tǒng)分析及仿真(第3版)
- CAXA CAD電子圖板2020工程制圖
- 邊做邊學(xué):Photoshop CS6 圖像制作案例教程
- Blender 3D 2.49 Architecture, Buidlings, and Scenery
- 人臉識(shí)別算法與案例分析
- WordPress Theme Design
- BackTrack 5 Wireless Penetration Testing Beginner's Guide
- 量化投資與FOF投資:以MATLAB+Python為工具
- PowerPoint 2016實(shí)戰(zhàn)從入門到精通(超值版)
- Plone 3 Intranets
- 數(shù)碼攝影后期密碼Photoshop CC調(diào)色秘籍(第2版)
- 3ds Max 印象 影視粒子特效全解析
- Python 3 Web Development Beginner's Guide
- Revit建模進(jìn)階標(biāo)準(zhǔn)教程(實(shí)戰(zhàn)微課版)
- Photoshop CC 2018基礎(chǔ)與實(shí)戰(zhàn)教程(全彩版)