(Print this page)

Creating and using Microsoft Azure Storage in .Net
Published date: Tuesday, April 22, 2014
On: Moer and Éric Moreau's web site

Microsoft Azure is offers a wide range of features. This month, I decided to play with blobs stored in storage containers. By play, I mean to create, delete, list, upload, and download files in a storage container.

Why use Azure storage when we have many other means of storing data? There are many reasons but let’s just say that Azure is:

  • highly Scalable being that a single storage account can store hundreds of terabytes of data
  • Durable because every file stored is automatically backed up once in the same container and also in another data center not in the same geographic region

Storage, containers, blobs

I think it is worth to start with an analogy trying to explain what these 3 terms are.

Microsoft Azure offers a set of features and storage is one of them. A storage can be seen as a drive in the cloud.

On this drive, you have folders. In Azure terminology, these folders are called containers. Apparently there is no limit on the number of containers you can create.

Each container contains files which in Azure terminology are called blobs. Apparently there is no limit on the number of blobs in a container.

A blob is a Binary Large OBject. It will contain anything you want including text files, images, audio files, videos, and so on. Microsoft Azure storage can store two types of blobs. The most frequent is probably block blobs (used in the demo here) which can be as large as 200GB in size. The other type are called Page blobs and they can be up to 1TB in size. If you frequently modify ranges of bytes in your blob, page blobs might be more efficient.

Don’t forget that Azure is not free. Every file stored and the band with used to copy data will be charged. This amount might be very small compared to the benefits (like scalability and durability) provided by Microsoft Azure. If you have an MSDN subscription, it includes some credits that are normally more than enough to tip your toe in the Azure world. By default, if all the credits are spent, the services just stop until the following month.

What you need

If you want to play with Microsoft Azure, you will of course need an access to Azure. If you have an MSDN subscription, you have access to an Azure account.

You will also need to download the free Azure SDK which offers the tooling required to connect an application to Azure from the Microsoft web site.

The demo application

I have used Visual Studio 2013 to create this demo application because I use the latest Azure SDK. I chose a plain old Windows Forms application just to prove that you can use Azure from any platform, even the oldest ones.

To your application, you need to set a reference to Microsoft.Windows.Azure.Storage as shown in figure 1.

Figure 1: Setting the reference

And because I want to use the latest and greatest version of the SDK, I need to set the targeted framework to at least 4.5 as shown in figure 2.

Figure 2: Setting the targeted framework

Finding your connection string

As soon as you want to access an Azure account with an application, you need to provide a connection string (much like when you want to connect to a database). This isn’t just your user name and password asked by the portal when you connect with your browser.

To find this connection string, first connect to the portal, click the Storage section on the left menu and click on the Manage Access Keys (bottom-center of the portal). This will open a dialog like the one shown in figure 3.

From that dialog, you need to pick the storage account name and access key to replace the proper place holder in the following connection string (found in my OpenStorageClient method):

Const strConnection As String = "DefaultEndpointsProtocol=https;AccountName=Your_Storage_Account_Name;AccountKey=Your_Primary_Access_Key"

This connection string would be a perfect candidate to be stored in a configuration file. Check http://emoreau.com/Entries/Articles/2007/03/AppSettings-revisited.aspx if you need a refresher on that topic.

Figure 3: Getting the connection string from the portal

Building the application

The figure 4 shows the demo application in action which I invite you to download because I will not copy here all the code, only the important lines (most of the complete code is validation and null checking). As you can see, we have 6 buttons to demo the 6 features I wanted to play with.

Figure 4: The demo app in action

All these 6 features all have a point in common. They all need to get a reference to your storage. If you download the demo application, you will find that the 6 click event all call the OpenStorageClient method to get the instance of a CloudBlobClient using the connection string we built in the previous step.

The full code of the OpenStorageClient method reads like this:

Private Function OpenStorageClient() As CloudBlobClient
    Try
	Const strConnection As String = "DefaultEndpointsProtocol=https;AccountName=Your_Storage_Account_Name;AccountKey=Your_Primary_Access_Key"
        Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(strConnection)
        Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
        Return blobClient
    Catch ex As Exception
        MessageBox.Show("An error occured opening the Storage account: " + ex.Message)
        Return Nothing
    End Try
End Function

Be sure to modify the connection string in this method with your own because the one currently set won’t connect you anywhere!

Now that we have an object connected to our storage, many things can be done from within our application. The demo apps show a couple. The first thing might be to create a new container. This is as simple as:

Dim blobContainer As CloudBlobContainer = CreateContainer(blobClient, txtContainer.Text)

In which the CreateContainer method main parts reads like this:

Dim blobContainer As CloudBlobContainer = blobClient.GetContainerReference(containerName)
blobContainer.Create()
blobContainer.SetPermissions(New BlobContainerPermissions With {.PublicAccess = BlobContainerPublicAccessType.Blob})

If you want to delete a container, just call something like:

Dim isDeleted As Boolean = DeleteContainer(blobClient, txtContainer.Text)

In which the DeleteContainer method reads essentially like this:

Dim blobContainer As CloudBlobContainer = blobClient.GetContainerReference(containerName)
blobContainer.Delete()

Listing all existing containers can be done in a simple loop like this:

For Each cloudBlobContainer As CloudBlobContainer In blobClient.ListContainers()
    listBox1.Items.Add(String.Format("{0} - {1}", cloudBlobContainer.Name, cloudBlobContainer.Uri))
Next

Then if you want to list the content of a container, you first need to get a reference of the container (much like we did for the storage). To its simplest form, getting the reference to an existing container is a single line (found in the OpenContainer method):

Dim blobContainer As CloudBlobContainer = blobClient.GetContainerReference(containerName)

With this container reference, you can then call the ListBlobs method to go through the list of elements like this:

For Each item As IListBlobItem In blobContainer.ListBlobs()
    If TypeOf (item) Is CloudBlockBlob Then
        Dim blob As CloudBlockBlob = DirectCast(item, CloudBlockBlob)
        listBox1.Items.Add(String.Format("Block blob of length {0}: {1}",
                                         Get_Size_in_KB_MB_GB(blob.Properties.Length),
                                         blob.Uri))
    ElseIf TypeOf (item) Is CloudPageBlob Then
        Dim pageBlob As CloudPageBlob = DirectCast(item, CloudPageBlob)
        listBox1.Items.Add(String.Format("Page blob of length {0}: {1}",
                                         Get_Size_in_KB_MB_GB(pageBlob.Properties.Length),
                                         pageBlob.Uri))
    ElseIf TypeOf (item) Is CloudBlobDirectory Then
        Dim directory As CloudBlobDirectory = DirectCast(item, CloudBlobDirectory)
        listBox1.Items.Add(String.Format("Directory: {0}", directory.Uri))
    End If
Next

Uploading and downloading files

Exactly like in the previous samples, before being able to handle files, you will need an instance of a container (which itself requires an instance of a storage).

With these variables in hands, you can create a blob reference before uploading your file using this instance:

Dim file As CloudBlockBlob = blobContainer.GetBlockBlobReference(txtFileName.Text)
file.UploadText(txtFileContent.Text)

Downloading is as easy. The minimal version is only 2 lines of code:

Dim file As CloudBlockBlob = blobContainer.GetBlockBlobReference(txtFileName.Text)
txtFileContent.Text = file.DownloadText()

Notice that there is more than methods that handle Text. There are also methods for stream, file, and byte array. There are also Async methods that you should really use because you never know how long it will take when dealing with cloud storage.

Conclusion

The cloud has great benefits. Each of your current application independent of the .Net platform on which they are built can use Azure. Why not start taming that beast with one of its simplest feature, the storage? If you have an MSDN subscription, you have some credits to start playing with it for free.



(Print this page)