Jumping In To Windows Azure Storage Services





The community tech preview release of Windows Azure offers three different means of storing information “in the cloud.” These include Blob Storage, Queues, and Table Storage APIs. Each of these are often accessed through REST APIs, using the HttpWebRequest object. They can also be accessed through ADO Client Services.



At first glance, this seems quite tedious: you have to create a request, add headers, parse the response, possibly sign the request, and so on. However, there is a much easier way to access the storage services using a DLL shipped with the Windows Azure SDK.

Here are the steps to use the Storage Client DLL in your own Azure project. These steps assume you already have a working Windows Azure project and the necessary key for accessing the Storage Services. The example below will access queues. Similar techniques can be used for the other types of storage services. Note that much of this code can be found within the StorageClient project. This blog posting is meant to shorten the amount of time needed for someone new to Azure to start using these services. After jumping in, it would be very beneficial to go back to the StorageClient project and see exactly how this library wraps the HTTP calls.


1) Right-click on your web role project (or worker role) and add StorageClient.dll to your references. It should be located under {install path}\StorageClient\Lib\Bin\Debug\StorageClient.dll. If it is not, follow the instructions under the \StorageClient\readme.txt to build the application.

image


2) Add an <appsettings></appsettings> in your project, under the <configuration> tag. You will need to make sure that there are tags present for your AccountName, AccountSharedKey, and for each Storage endpoint you will be using. It will look something like this:

<add key = "AccountName" value="jtkirk"/>
<add key = "AccountSharedKey" value="rL+XAccN6J/Ie7AaiZoiC20JF8kwVedQyaCHYK/9b7x/Vw7VgkkVw7I3B3aQrIU7grMcpCuWnQpSsd9yfv3Ujw=="/>
<add key="QueueStorageEndpoint" value="
http://jtkirk.queue.core.windows.net"/>

3) Be sure to include the StorageClient namespace in your code file with the using (C#) / imports (VB.NET) statement.

using Microsoft.Samples.ServiceHosting.StorageClient;

4) Whenever you want to access the REST APIs for the queues, you will do so though classes in the StorageClient namespace. To do this, you will need to access a created queue service using your storage account credentials. This is a sample for adding a Queue with queuename being a variable holding the name of a queue.

StorageAccountInfo account = null;
QueueStorage queueService = null;

account = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
queueService = QueueStorage.Create(account);
queueService.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100));
MessageQueue q = queueService.GetQueue(queuename);

bool result = q.CreateQueue(out exists);
if (!exists && result)
{
lblQueueStatus.Text = "Queue " + queuename + " has been created.";
}
else
{
lblQueueStatus.Text = "Queue " + queuename + " has NOT been created.";

}

5) For other functions, take the same reference to the queueService and make direct calls against it. For example:


foreach (MessageQueue queue in queueService.ListQueues())
{
lbxQueues.Items.Add(queue.Name);
}

Similarly, you can search for Queues with certain prefixes, clear queues out, delete queues, peek at values in each queue and so on.

And that’s a quick way to jump into Azure Storage Services. You can find out more about about Windows Azure, and other Architectural Guidance topics from RDA through this blog and through Twitter.

RDA Architecture Twitter: rdaarchitecture
Personal Twitter: ipockrda

0 comments: