In part one of this article, I walked through creating an Azure account and detailed how the Portal is set up. I also specifically explained how to create a storage account. Then, I just left you with this new toy and no explanation on how to use it. Well, here is the rest of the story. In this article, I will discuss the code needed to insert data into your storage account as well as some third-party tools that can help you view and download the files from your account. Let’s jump right in!
The Code. Ssshhhhhh!
As explained in the first part of this article, I created a Windows service to push my photos up to my account whenever I synced any camera or phone. I created this service so that everything would occur behind the scenes without requiring me to do anything. One could write this as a Windows form or anything else for that matter and the code that performs the inserts should remain the same. I am not going to explain every detail of the service, but rather focus on the code needed to push something into the storage account and what information you will need from your storage account to do so. Below is the code to handle the inserts. I will break this down into smaller sections and explain what’s going on in each section. A couple of notes on the code, I used Visual Studio 2010 to create this code. When you are coding be sure your target framework is .NET Framework 4 and not the Client Profile. Also, you will need to include a reference to Microsoft.WindowsAzure.StorageClient — this isn’t available under the client profile settings.
The first section of code uses the information required to gain access to the storage account. In part one, I mentioned briefly about the Manage Keys section at the bottom of the storage account dashboard window. In that area, you want to get the Storage Account Name and the Primary Access Key values, then take those values and create a connection to your storage account.
At this point, you have set up the connection, but you also need to tell it where to put the files you are going to insert. The next step in the process determines what container to insert the files into. You can think of a container as a folder within your storage account and you can have multiple containers in a storage account. One thing to remember is that there are no other levels with containers; each container is like a large folder with never ending space, at least until you hit the 100TB limit. The code below checks to see if the specific container exists and if not it creates it. Then it sets the connection to the specified container.
The next code section is where you create the blob object that you are going to upload to the storage account. Above, I mentioned that a container doesn’t have any file structure. So what you can do to handle this is incorporate the folder structure in the name of the blob object with the line of code here: strFileName = fileNamePath.Replace(“\\”, “/”);
And finally the code below is where we upload the blob object to the storage account.
That’s all there is to uploading blob objects to a storage account. In my case, that’s pretty much the end of the story since I am using this process to simply backup all my family pictures. But in the event I want to view any of the files I’ve uploaded or even download some of them, there are several options. I could create some code to pull the objects down for me or use some third part tools to do it. I chose the latter since I would only need to do this in the case of an emergency which I hope never happens.
There are several third-party viewers you can choose from. The one I use is called Azure Storage Explorer. There is no particular reason I use it other than it was one I found and it did what I needed. Also, it’s free from CodePlex. There are some more advanced solutions that are available at a cost, but with most paid versions, you simply enter the storage account name and the access key you used in the code above. It can then access the account and list the files you have uploaded. And many of the software packages give you the ability to download the files to your computer if you would like. Some also recognize the renaming of the blob object with the folder structure in the name and will allow you to drill through the files as you would with explorer on your desktop. I would recommend researching which option best fits your needs and go with that. Since Azure is being used more widely, your options for these solutions will likely become more abundant.
In these two articles I have explained how easy it is to not only create an Azure account, but to set up a storage account as well. Also in this last article I hope I have shed some light on how little code is actually needed to upload blob objects up to a storage account, and with the help of some third-party tools, pull them back down if needed. I focused on just a single aspect of using a storage account, but there are many other options as well. Also there are several other options within Azure; it is a very powerful tool. If you found this helpful, please let me know below or connect with me on Twitter, I’d love to hear from you!