Storing and Retrieving Serialized Data with Azure Blob Storage

On a project I am currently working, we have some normalized data which we need all instances of our Worker Role to have access to. This data when denormalized for the table structure we use requires us to take each row and turn it into about 50,000 rows. Because of this we obviously don't want to denormalize it and store it in Azure's table storage. We eventually will be doing this, but we need to work with it before denormalizing it.

The plan we came up with is to serialize the data and store it as a blob in Azure storage. We tried it a few different ways running into snags in what seemed to be all directions. I just figured out how to get it to work. Finally.

First we need to check and see if the blob exists, and if it does we need to obtain the data from it.

var blob = BlobStorage.Create(
        StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration()).
        GetBlobContainer("myblobcontainer");
 
var encoding = new ASCIIEncoding();
 
if (blob.DoesBlobExist("myblob"))
{
    Stream stream = new MemoryStream();
    var contents = new BlobContents(stream);
    blob.GetBlob("myblob", contents, false );
    
    string myXml = encoding.GetString(contents.AsBytes());
    
    StringReader stringReader = new StringReader(myXml);
    var reader = new XmlTextReader(stringReader);
 
    var deserializer = new XmlSerializer(typeof(List<MyObject>));
    var myDataObjects = (List<MyObject>)deserializer.Deserialize(reader);
}

Second we need to work with the data.

// Do a whole bunch of work here.
// This stuff is really important.
// Did you really just read a block
// of comments in a blog?
 
// I am being honest when I say that
// these blocks of comments are
// not really needed at all, but
// wasn't it fun reading this pointless
// block?

Third we put the data back with updates

var serializer = new XmlSerializer(typeof(List<MyObject>));
var stringBuilder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(stringBuilder);
serializer.Serialize(writer, myDataObjects);
 
byte[] theseBytes = encoding.GetBytes(stringBuilder.ToString());
 
blob.CreateBlob(
       new BlobProperties("myblob") { ContentType = "text/xml" },
       new BlobContents(theseBytes),
       true);

Hooray! We're now able to serialize to Azure Blobs.

Comments