- 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.
- 3ds Max 2014標(biāo)準(zhǔn)教程(全視頻微課版)
- 中文版AutoCAD 2015實(shí)用教程
- Moodle 1.9 for Teaching 7/14 Year Olds: Beginner's Guide
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)移動(dòng)UI設(shè)計(jì)
- 中文版Illustrator CC 2018基礎(chǔ)培訓(xùn)教程
- 邊做邊學(xué):平面廣告設(shè)計(jì)與制作(Photoshop 2020+Illustrator 2020·第3版·微課版)
- 中文版CorelDRAW X6基礎(chǔ)培訓(xùn)教程(第2版)
- Stable Diffusion圖像與視頻生成入門(mén)教程
- MATLAB R2024a完全自學(xué)一本通
- Origin 2022科學(xué)繪圖與數(shù)據(jù)分析
- 中文版Premiere Pro CS6視頻編輯(慕課版)
- Oracle 10g/11g Data and Database Management Utilities: LITE
- UG NX 11中文版基礎(chǔ)教程
- 全鏈路UI設(shè)計(jì):創(chuàng)意思維+項(xiàng)目實(shí)戰(zhàn)+就業(yè)指導(dǎo)
- 案例學(xué):Photoshop電商美工設(shè)計(jì)