Windows Azure Storage, Part 1

Software applications often need to store data and applications written for Windows Azure are no exception. Windows Azure provides three types of storage: queues, blobs and tables. As we have already seen something about queues in a prior post we now turn our attention to blobs and tables. Part 1 will cover blobs and Part 2 will cover tables.
  
Note: SQL Azure (part of the Azure Platform) provides relational database capability. SQL Azure will be discussed in a later post.
  
Overview
  
Azure storage is based on REST. All storage objects are available through standard HTTP calls to a named URI. In practice, for .NET developers, though, it is probably easiest to just use the StorageClient assembly included as of version 1.1 of the Azure SDK.
  
It is worthwhile mentioning that all objects stored in Windows Azure are fault-tolerant. Behind the scenes everything is replicated three times. If a corruption is detected Azure can transparently fail-over to a replicated copy. In addition Azure makes a backup copy of our data in a separate data center. That way even if there is a catastrophic event which takes out the main data center our data remains intact. Having these automatic backups is an example of how Azure frees the developer from certain administration and operational details.
  
Blobs
  
Blobs are the simplest type of data storage. Essentially a blob is just a sequence of bytes; it can be anything the application needs it to be. It can be a photo of your mother-in-law, a word processing document, a zip archive containing the entire contents of your Megadeth music library … anything.
  
It is sometimes believed that the term BLOB is an acronym for Binary Large Object although some disagree with that. In any case the word originally came from database terminology. An Azure blob is similar to, but slightly different from that usage.
  
For example: 

  • An Azure blob can have metadata associated with it. Metadata is often useful in providing information about the data (such as the name of the person in a photo) and for searching. 
  • Azure blobs must exist in “containers”. Containers can hold one or more blobs but a container is not necessarily a database. It is useful to think about containers and blobs using the familiar (to most!) folder and file metaphor. A container is like a folder; a blob is like a file. Containers can also have associated metadata.

 So, briefly, how might some of this look in code?
  
If we use Visual Studio to create a Cloud Service project, much of the work (setting up the references, etc.) is taken care of for us.
Just make sure we have these using statements in our source file:  

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

Then we can begin creating containers and blobs in our application.
  
As a very simplified, minimalistic example we could create a container and then create a blob in that container. In this example our blob is nothing more than a hard-coded array of bytes but, as mentioned, it could be any kind of binary data we can access in our code.
  
private void MySimpleBlobMethod()  
{  
    // create a container in the blob storage account  
    var myStorageAccount = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);  
    var myBlobStorage =myStorageAccount.CreateCloudBlobClient();  
    var myContainer = myBlobStorage.GetContainerReference(“mycontainer”);  

    myContainer.CreateIfNotExist();  

    // upload an array of bytes into a blob in that container  
    // here the byte array is just a random sequence of bytes  
    // in real-life it would be the binary data you are interested in  
    byte[] myBytes = {255, 1, 0, 234, 6, 12, 49, 52, 97, 53};   
    var myBlob = myContainer.GetBlockBlobReference(“myBlob”);  
    myBlob.UploadByteArray(myBytes);  
}
 (Click here for demo)

In Learning Tree’s Windows Azure Course we will have hands-on exercises where you get to work with blobs in a much more realistic scenario. This simple introduction was only intended to show the bare essentials of working with Azure blobs!  

Kevin

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.