Click here to Skip to main content
Click here to Skip to main content
Go to top

MyBlobber: Access Windows Azure Storage Blobs

, 20 Nov 2009
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

bhavik1st
Architect Symantec
India India
Software Architect
 
- Instrumental in transforming various concepts to successful concrete products/features.
- Passionate about quality in everything from the requirement specification to the end product.
- Involved with wide range of technologies and domains.
- Likes to think from customer or end user perspective.
 

Specialties: Architecture & Design, Web Services, Cloud Computing, C++, COM, C#, .NET
Clouds: Microsoft Azure, Amazon, Google App Engine
Other Interests : Linux, Java, HTML, PHP, Javascript and other cross platform technologies.
 

Connect with me @ LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 PinmvpKanasz Robert28-Sep-12 5:42 
GeneralMy vote of 2 PinmemberSingleDad14-Mar-11 6:54 
GeneralRe: My vote of 2 Pinmemberbhavik1st25-Apr-12 20:50 
GeneralObject reference not set to an instance of an object. Error Occured Uploading test.txt!! Pinmembersholchi4-Dec-09 10:44 
AnswerRe: Object reference not set to an instance of an object. Error Occured Uploading test.txt!! [modified] Pinmemberbhavik1st4-Dec-09 11:22 
Questionupload to where, download from where? PinmemberUnruled Boy3-Dec-09 1:45 
AnswerRe: upload to where, download from where? Pinmemberbhavik1st4-Dec-09 3:17 
NewsBlobber on CodePlex Pinmemberbhavik1st21-Nov-09 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by bhavik1st
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid