- 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.
- Joomla! 1.5 Site Blueprints
- GIMP 2.6 cookbook
- Illustrator實例教程:Illustrator 2021(電子活頁微課版)
- Photoshop CC 2018實用教程
- Excel 2013使用詳解(修訂版)
- Inkscape 0.48 Illustrator's Cookbook
- 中文版3ds Max 2016實用教程
- IT Inventory and Resource Management with OCS Inventory NG 1.02
- SolidWorks 2018有限元:運動仿真與流場分析自學(xué)手冊
- PowerPoint 2013從新手到高手(超值版)
- 新編中文版3ds Max 2016入門與提高
- Instant Flask Web Development
- Microsoft SQL Azure Enterprise Application Development
- 中文版AutoCAD 2022從入門到精通
- Magento 1.3 Theme Design