Windows Azure – Blob Storage and Tables

Along with SQL Azure (The relational data store) Windows Azure provides several options for non-relational storage.
Blob Storage: For storing images, videos, documents, and other larger binary or text data.
Table Storage: Key-value storage for non-relational data
Queue: Messaging

Microsoft is currently offering several options to experiment with Azure. Most MSDN subscriptions come with a certain amount of free time to use, and there is also a free one month trial available:
Free Trial

I also downloaded an Azure Client from NuGet.

I used the free trial to investigate the Blob and Table storage. I found a good tutorial here.

One you’re signed in, you can go to the ‘Storage’ tab to create a ‘Storage’ account, which includes Blob, Queue and Table storage. You’ll select an account name, and that will be used for three URLs, each an endpoint for each type of storage. An account name and a key are used to access storage.

For the Blobs, the records can be organized into containers (similar to tables). The Container name must be lower case.
For Table storage, the account is sub-divided in tables, which store entities. Entities have properties, which are key-value pairs. When creating an entity, you’ll need to specify values for the Row Key and for the Partition Key. These two values will uniquely identify a record within a table. There is also a timestamp property. The code Entity should inherit from TableEntity.

Here is a short code example to create a container for, upload and retrieve an entity for both the Blob and the Table Storage.

using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.IO;

namespace AzureStorage
{

    class Program
    {
        static void Main(string[] args)
        {
            // Credentials
            string accountName = "AccountName";
            string accountKey = "key";
            StorageCredentials credentials = new StorageCredentials(accountName, accountKey);

            BlobExample(credentials);
            TableExample(credentials);

            Console.WriteLine("Completed...");
            Console.Read();
        }

        private static void BlobExample(StorageCredentials credentials)
        {
            Uri uri = new Uri("http://accountname.blob.core.windows.net");
            CloudBlobClient blobClient = new CloudBlobClient(uri, credentials);

            // Blob Container - Name must be lower case
            Console.WriteLine("Create Blob Container");
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("testcontainer");
            blobContainer.CreateIfNotExists();

            // Upload Blob
            Console.WriteLine("Upload Blob");
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference("Photo1");
            string photoPath = (@"C:\Photo1.jpg");
            using (var fileStream = File.OpenRead(photoPath))
            {
                blockBlob.UploadFromStream(fileStream);
            }

            // Retrieve Blob
            Console.WriteLine("Retrieve Blob");
            foreach (IListBlobItem item in blobContainer.ListBlobs(null, false))
            {
                CloudBlockBlob blob = (CloudBlockBlob)item;
                Console.WriteLine("Blob: " + blob.Name);
            }
        }

        public class FootballPlayer : TableEntity
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Position { get; set; }

            public FootballPlayer(int jerseyNumber, string teamName)
            {
                this.PartitionKey = teamName;
                this.RowKey = jerseyNumber.ToString();
            }

            public FootballPlayer(){}
        }

        private static void TableExample(StorageCredentials credentials)
        {
            Uri uri = new Uri("http://accountname.table.core.windows.net");
            CloudTableClient tableClient = new CloudTableClient(uri, credentials);

            // Create table
            Console.WriteLine("Create table");
            CloudTable table = tableClient.GetTableReference("footballplayer");
            table.CreateIfNotExists();

            // Create entity 
            Console.WriteLine("Create Entity");
            var entityMR = new FootballPlayer(2, "Falcons");
            entityMR.FirstName = "Matt";
            entityMR.LastName = "Ryan";
            entityMR.Position = "QB";

            // Insert Record
            Console.WriteLine("Insert Record");
            TableOperation insertOperation = TableOperation.Insert(entityMR);
            table.Execute(insertOperation);

            // Retrieve entities
            Console.WriteLine("Retrieve Record");
            TableOperation retrieveOperation = TableOperation.Retrieve<FootballPlayer>("Falcons", "2");
            FootballPlayer player = (FootballPlayer)table.Execute(retrieveOperation).Result;

            Console.WriteLine("#" + player.RowKey + " " + player.FirstName + " " + player.LastName);
        }
    }
}

Advertisements

2 Responses to Windows Azure – Blob Storage and Tables

  1. […] haven’t checked out Azure in a while (it was about 2 1/2 years ago since my last Azure post ), so I jumped back on to see what was different. These are more notes that I took than a complete […]

  2. […] haven’t checked out Azure in a while (it was about 2 1/2 years ago since my last Azure post ), so I jumped back on to see what was different. These are more notes that I took than a complete […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: