Brendan Enrick

Daily Software Development

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.

First Post?

In classic forum style this is not the first post, but it is close. Just trying to get some content to take up some space on the front page of my new blog. Yes, you heard correctly. NEW BLOG! WOOHOO!!!

Blog Moving Here

Ladies, gentlemen, boys, girls, and anyone who doesn't fit in any of those categories this includes, ruffians, riffraff, hooligans, and probably some other people listen up. I have wonderful news to announce. I am finally moving my blog off of aspadvice.com, the site which has graciously given my blog a home for over two years now.

I welcome everyone to this new experience known for now as...... Brendan Enrick's Blog.

What does this mean to you? Well it means that if you ever visit my blog and read a post you'll have a nice look and feel. It also means better integration for me using Windows Live Writer.

Will any of what you've said above change? Yes, of course it will change. Doesn't everything? Perhaps I will switch to another program for writing my blogs. Until that day comes though who knows.

All crazed lunacy aside, this is my new blog. I hope everyone likes it and enjoys reading it.

Thank you and good night........ um....... I mean morning. It is after all 9:25 AM here right now.