Skip to main content

Using Azure blob storage

March 06, 2014 mgroves 0 Comments
Tags: azure blob csharp

Even though I've been using Windows Azure for quite some time, a lot of the functionality is unexplored territory to me.

Recently, I took a look at Azure Blob Storage, and I was surprised by how simple it was.

Blob storage is simply a way to store stuff (mainly files). That's about the long and short of it. Here's how to get started:

  1. Create a new storage service: on your dashboard, click "+ NEW", then Data Services, then Storage. Pick a URL subdomain, region, and subscription. There you go.
  2. In that service, create one or more containers. This is basically an organizational structure, kinda like a folder. Containers can be public or private.
  3. Put stuff in the container. Each item of "stuff" in this container is called a blob, and each of these items have their own URL.

I don't know of a way to directly upload blobs right from the Azure control panel (yet), but it's very simple to do with some C#. Just add Windows Azure Storage to your project with NuGet (Install-Package WindowsAzure.Storage).

You'll need to create a "connection string" to your storage service. The connection string is just:

DefaultEndpointsProtocol=https;AccountName=yourServiceName;AccountKey=yourAccountKey

Where yourServiceName is whatever your named your service. yourAccountKey can be grabbed right from your Azure Dashboard. Click on the service name and then click "Manage Access Keys" at the bottom of the screen. Copy "Primary Access Key" and paste it in there.

Now you're ready to write some C#. Here's a simple class that I wrote that lets you check for existence of a blob (by name), upload a new blob given a name and a stream (from a file uploaded to a web site, for instance), and a way to delete a blob (again, by name).

It's really that simple. Once you upload some files, go back to your Azure Dashboard and take a peek in the container. You'll see all the blobs there, as well as URIs to get to them.

It's so easy, that I've already moved the images for this site (Cross Cutting Concerns) over to Azure Storage (previously I was storing them off-site with traditional hosting).

Windows Azure dashboard blob container

Some drawbacks that I've noticed, and how I dealt with them:

  • You can get a List of blobs using WindowsAzure.Storage, but you can't really get a total count (at least not without iterating through the whole list). So if you are building a tool to manage these blobs, you may need to store some metadata (like name, uri) in a local database or something (which is what I did for this site). I believe there are already a bunch of tools out there to access containers/blobs that you can use as well: you don't have to build your own.
  • There's no "transaction" or "rollback" when doing blob transactions. You'll need to handle that case yourself. In my case, if something goes wrong with other parts of the "transaction", I'll use DeleteIfExists to remove the blob. Not the most elegant, but it works.

 

Comments

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter