Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

MyBlobber: Access Windows Azure Storage Blobs

4.00/5 (4 votes)
20 Nov 2009GPL33 min read 50.8K   580  
A simple C# command line utility to demonstrate access to Windows Azure storage.

Introduction

This article deals with Windows Azure Cloud Storage. I was looking for a simple way to demonstrate the storage fundamentals of Windows Azure. That's when the idea of a simple command line tool to exchange data with Azure Storage struck me. Another benefit we can have with a command line tool is we can write a simple batch file, or a Windows 7 PowerShell script, which would invoke this tool to periodically take the backup of important files to Windows Azure Cloud Storage.

Windows Azure Storage Blob

Background

This is my second article in the series of articles related to Windows Azure - A cloud computing initiative of Microsoft. You may want to read the first article Hello Windows Azure before proceeding here.

Windows Azure Storage

Windows Azure Storage provides mainly three types of storage:

  • Blob - For storage of files/information chunks/large data buffers (Blob = Binary Large Object).
  • Tables - Massive, scalable, structured storage.
  • Queues - For FIFO storage/dispatch mechanism/asynchronous communications.

We are going to have a more detailed look at Blob Storage which provides a way to store files and can be used as backup storage for important files. To access it, you need a storage service account with Windows Azure, which you can sign up at www.azure.com. APIs to access storage service are RESTful, and work over HTTP/S. Blob storage is organised into containers which act as folders to hold files and can have either public or private accessibility. Information in public containers can be accessed via URIs like http://[account].blob.core.windows.net/[container]/[file]. For example: http://bhavik.blob.core.windows.net/mycontainer/test.txt. Apart from blob, other metadata related to blob can be stored as key:value pairs. The image below shows the organisation of blob.

Windows Azure Storage Blob

* Image courtesy http://blogs.msdn.com.

Blobber Code

The code is compact, simple, and self-explanatory. All we need to understand is some simple concepts of command line argument parsing and Application Config settings in C#. To access Azure storage, it uses StorageClient.dll provided in the samples of the Azure SDK. Typically, you may find it at "C:\Program Files\Windows Azure SDK\v1.0\samples.zip\StorageClient". The StorageClient.dll wraps the Azure Storage API as described in MSDN.

The following code blocks simply demonstrate upload and download of data from Blob:

C#
// Code to upload file to Blob
try
{
    //Create Storage Account Info
    StorageAccountInfo StorageAcc = new StorageAccountInfo(
        AccessUri,
        null,
        (String)ConfigurationSettings.AppSettings["AccountName"],
        (String)ConfigurationSettings.AppSettings["AccountSharedKey"]);


    // Container names have the same restrictions as DNS names
    BlobStorage blobStorage = BlobStorage.Create(StorageAcc);
    BlobContainer blobContainer = null;
    blobContainer = blobStorage.GetBlobContainer(
       ConfigurationSettings.AppSettings["ContainerName"]);
    if (false == blobContainer.DoesContainerExist())
    {
        //Verbose("Container does not exist! Creating One.");
        blobContainer.CreateContainer(new NameValueCollection(), 
                                      ContainerAccessControl.Private);
    }

    // Create metadata to be associated with the blob
    NameValueCollection metadata = new NameValueCollection();
    metadata["FileName"] = File;
    metadata["Submitter"] = "Blobber";

    //Create Blob Properties object
    BlobProperties properties = new BlobProperties(File);
    properties.Metadata = metadata;
    properties.ContentType = MimeType(File);

    // Create the blob
    byte [] FileContents = System.IO.File.ReadAllBytes(FileName);
    BlobContents fileBlob = new BlobContents(FileContents);
    blobContainer.CreateBlob(properties, fileBlob, true);
}
catch (Exception e)
{
    Console.WriteLine(e.Message.ToString());
    bRetval = false;
}

// Code to download file from Blob
try
{
    //Create Storage Account Info
    StorageAccountInfo StorageAcc = new StorageAccountInfo(
        AccessUri,
        null,
        (String)ConfigurationSettings.AppSettings["AccountName"],
        (String)ConfigurationSettings.AppSettings["AccountSharedKey"]);

    // Container names have the same restrictions as DNS names
    BlobStorage blobStorage = BlobStorage.Create(StorageAcc);
    BlobContainer blobContainer = null;
    blobContainer = blobStorage.GetBlobContainer(
       ConfigurationSettings.AppSettings["ContainerName"]);

    //If Container or Blob Does not exist exception shall be thrown
    FileStream fs = File.Create(FileName);
    BlobContents fileBlob = new BlobContents(fs);
    BlobProperties propBlob;
    propBlob = blobContainer.GetBlob(sFile, fileBlob, true);
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
    bRetval = false;
}

Blobber Usage

The usage of Blobber is pretty simple. We need to set the default account name, access key, and container name in blobber.config, which is the application config file. The sample configuration XML can be downloaded here - Blobber.exe.config.

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
<!-- Remove this if not using local development machine -->
    <add key="AccountName" value="devstoreaccount1"/>
    <add key="AccountSharedKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="/>
    <add key="BlobStorageEndpoint" value="http://127.0.0.1:10000/"/>
<!--    
    <add key="AccountName" value="PUT YOUR USER NAME HERE"/>
    <add key="AccountSharedKey" value="PUT YOUR KEY HERE"/>
    <add key="BlobStorageEndpoint" value="http://blob.core.windows.net"/>
-->
    <add key="ContainerName" value="mycontainer"/>
  </appSettings>
</configuration>

Once set, we may simply use Blobber from the command line. Example usage:

Blobber -u test.txt

Argument options:

  • '-u' or '-d <file>': Specifies the filename to upload or download. Filename with spaces should be enclosed in "double quotes".
  • '-v' <0/1>: Verbosity 0 (Off: Default) or 1 (On). This argument is optional, and should be first if used.

Sample output:

Windows Azure Storage Blob

History

  • 21-Nov-2009: Did not get much time since last article. Finally found a Saturday to upload v0.1.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)