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.


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.

Image from

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");

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);


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.

Image from

How To

CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();

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

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 ...


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

Image from

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);

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

Managing and Exploring

You can explore and manage your stored data with the Azure Storage Explorer. It's avaible for free at

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


No comments yet.