Windows Azure – Using DataSet with cloud storage

On Windows Azure CTP some file system operations are not allowed. You cannot create and modify files located on server hard disc. I have small application that stores data to DataSet and I needed place where I can hold this file. The logical choice is cloud storage service of course. In this posting I will show you how to read and write DataSet to your cloud storage as XML-file.

Although my original code is more complex (there are other things you have to handle when using file based storage in multi-user environment) I give you here the main idea about how to write some methods to handle cloud storage files. Feel free to modify my code as you wish.

Before we start

Windows Azure blob storageYou need Windows Azure storage account. If you don’t have it currently then go to this page and register yourself. When your account is ready you must create your cloud storage service in Windows Azure portal. Also you need Windows Azure Tools for Visual Studio. Now create a simple application and add reference to StorageClient.dll file (it is shipped with Azure tools, you can search for it from Program Files folder if you cannot find it fast).

The structure of cloud storage is simple. At the root level you have your account. Under your account you have services – Table, Queue and Blob storage. We are using Blob storage in this example. Blob storage has containers at root level. You can handle containers as independent root level folders. Each container may contain one or more BLOBs – handle BLOBs as files. Screenshot here should illustrate it somehow. _blob is the name of my cloud storage service (it is name that I use in my client software). dataset is the name of container and Data.xml is my data file.

Connecting to cloud storage

As we are working with same file in same container we can say that we need container as root level object. We can easily write method that connects to cloud storage and returns us container we need.

private BlobContainer GetContainer()
{
   
var account = new StorageAccountInfo
(
                           
new Uri("http://<ACCOUNT>.blob.core.windows.net/"
),
                           
null
,
                           
"<ACCOUNT>"
,
                           
"<KEY>"
                        );
   
var storage = BlobStorage
.Create(account);
   
var container = storage.GetBlobContainer("dataset"
);

   
return container;
}

This method is private because I don’t want to expose cloud storage details to other classes.

Loading DataSet

Now let’s read our data file from cloud storage and let’s load it to DataSet. There is one little trick you should be aware of. Take a look at the following method and notice that I have to handle the location of stream pointer.

public void Load(DataSet data)
{
   
var
container = GetContainer();
   
using (var mem = new MemoryStream
())
    {
       
if (!container.DoesBlobExist("Data.xml"
))
           
return
;

       
var fileBlob = new BlobContents
(mem);
        container.GetBlob(
"Data.xml", fileBlob, true
);
       
var
stream = fileBlob.AsStream;

       
// Required - stream pointer must be at position 0
        stream.Seek(0, SeekOrigin.Begin);

        data.ReadXml(stream);
    }
}

Saving DataSet

After changes it is good idea to write data back to cloud storage at some moment. Here is the method that saves new file with fresh data to cloud storage container.

public void Save(DataSet data)
{
   
var
container = GetContainer();
   
if (container.DoesBlobExist("Data.xml"
))
        container.DeleteBlob(
"Data.xml"
);

   
var metadata = new NameValueCollection
();
    metadata[
"FileName"] = "Data.xml"
;

   
var properties = new BlobProperties("Data.xml"
)
    {
        Metadata = metadata,
        ContentType =
"text/xml"
    };

   
using (var mem = new MemoryStream
())
    {
        data.WriteXml(mem);
       
var fileBlob = new BlobContents
(mem.ToArray());
        container.CreateBlob(properties, fileBlob,
true);
    }
}

Conclusion

Using Windows Azure cloud storage and BLOBs is pretty simple if we use right tools. StorageClient saved us some valuable time and we were able to write some methods that are very easy to use for other programmers. You can easily change those methods so accoun information is read from application configuration and programmers can also specify container and file they need from cloud storage.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

    Your email address will not be published. Required fields are marked *