Azure Storage

The Windows Azure Storage services provide persistant, redundant storage. Each Storage Account has access to three different kinds of services: blob, queue and table. All the required classes are in the Microsoft.WindowsAzure.StorageClient namespace.

Blob

The Blob service provides storage for binary and text files. Each blob must be located in a Container (like a folder) and can have metadata associated.

azure_blob.png
Image from http://blogs.msdn.com/b/jnak/archive/2010/01/11/walkthrough-windows-azure-blob-storage-nov-2009-and-later.aspx

How To

First you have to create a CloudBlobClient with you storageAccount object:

CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();

Then, you'll have to create (or get, if already exists) your blob container

CloudBlobContainer container = blobStorage.GetContainerReference("CONTAINER_NAME");
container.CreateIfNotExist();

Storing/Retrieving Data

var name = string.Format("{0}_{1}.txt", upload.FileName, Guid.NewGuid());
var blob = GetResultsContainer().GetBlockBlobReference(name);
blob.Properties.ContentType = GetMimeType(upload.FileName);
blob.UploadFromStream(upload.FileContent);

Queue

The Queue service provides asynchronous message passing between roles. The queue works like a producer-consumer: roles can add messages to it or read messages if they're avaible. Each Storage Account can have unlimited queues (indendified by a string name), and each queue can have unlimited messages. However, each message can have at most 8kb.

azure_queue.jpg
Image from http://msdn.microsoft.com/en-us/library/gg433127.aspx

How To

CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();

CloudQueue queue = queueStorage.GetQueueReference("QUEUE_NAME");
queue.CreateIfNotExist();

Sending Messages

queue.AddMessage(new CloudQueueMessage("YOUR MESSAGE HERE (MAX 8KB)"));

Receiving Messages

To retrieve messages from the queue, you just need to get a message from it and test against null value. After processing the message, delete it from the queue.

CloudQueueMessage msg = queue.GetMessage();
if (msg != null)
{
	string message = msg.AsString;
	// ... do something ...
	queue.DeleteMessage(msg);
}

Table

The Table service provides structured, non-relational storage. The same table can contain entities of different kinds.

azure_table.png
Image from http://blogs.msdn.com/b/jnak/archive/2008/10/28/walkthrough-simple-table-storage.aspx

How To

It's a little more tricky than the Blob and Queue services. To use the Table service you must first add a reference to System.Data.Services.Client to the Role that will use it. To make that, right click the desired role (it can be either a web or a worker role), click Add Reference... and choose System.Data.Services.Client in the .NET tab.

TableServiceContext tableServiceContext = new TableServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);
storageAccount.CreateCloudTableClient().CreateTableIfNotExist("TABLE_NAME");

Creating Models

Now that you've added the reference and know how to create a table, you just need to create models for the entities you will store there.

using Microsoft.WindowsAzure.StorageClient;

public class PersonModel: TableServiceEntity {
    public PersonModel(string partitionKey, string rowKey) : base(partitionKey, rowKey) { }

    // Rows need a unique partition key – so create a new guid for every row
    public PersonModel() : this(Guid.NewGuid().ToString(), String.Empty) { }

    // My properties
    public string Name { get; set; }
    public int Age { get; set; }
    // ... other properties that will be stored ...
}

Adding Entities

tableServiceContext.AddObject("TABLE_NAME", new PersonModel() {
    Name = "Jack",
    Age = 30
});

Fetching Entities

IEnumerable<PersonModel> results = tableServiceContext.CreateQuery<PersonModel>("TABLE_NAME").Execute();

Library Reference

http://msdn.microsoft.com/en-us/library/ee393295.aspx

Managing and Exploring

You can explore and manage your stored data with the Azure Storage Explorer. It's avaible for free at http://azurestorageexplorer.codeplex.com/.

Last edited Mar 15, 2011 at 7:53 PM by bjurkovski, version 7

Comments

No comments yet.