Working with most data storage technologies requires us to connect to some other source be it a database, or in the last few years, some sort of cloud based storage. As I was working through my latest project I decided to give Azure storage a shot for all of my images, and it was a quite a fun experience. However, most concerning was having to have a magic string inside my code somewhere. Here is how I solved the problem, but using the web.config connection string, it is super simple.
We are going to be using blob storage example for this case, but I am sure it will work for tables and queues. I will have to say that I haven’t worked with tables or queues yet so I am not sure how their connection string looks.
First, we need to set a connection string in the web.config file. It is just like a connection string for anything else and since it is all based on strings, in the web.config, it is simple for Azure connection information too.
<add name="AzureBlob" connectionString="DefaultEndpointsProtocol=http;AccountName=someAccountName;AccountKey=accountKeyGoesHere" />
Lets look at the BLOB connection string here is an example.
DefaultEndpointsProtocol=http;AccountName=someName;AccountKey=accountKeyHere
Notice there are 3 parts
- DefaultEndpointsProtocol – this is the protocol to use http or https to make the connection
- AccountName – you set this for your specific account you wish to access that you created
- AccountKey – this is a really long key that is generated for you that acts like a password
Here is a piece of code that I use to get my container from my BLOB storage account.
private CloudBlobContainer GetContainer()
{
return CloudStorageAccount
.Parse(this.connString)
.CreateCloudBlobClient()
.GetContainerReference(containter);
}
The key to this piece of code is the Parse() method. Using that method you can pass in a connection string and use that to connect if you connecting to azure outside of a webrole or a workerrole. If you are using a webrole or workerrole you will probably be running it inside an Azure project which comes with its own configuration information. If you are using the Azure configuration you can use the FromConfigurationSetting() method instead of Parse(), which is much more convenient.
I set out to figure this particular thing out because all I could seem to find were examples using the FromConfigurationSetting() method, but I wasn’t using an Azure project so I need a way to go outside of Azure. Most example code I saw was like this:
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
I actually setup a custom class since the images are only a small part of my project, so here is a little bit of it as an example
public class AzureHelper
{
private string connString;
private string containter;
public AzureHelper(string connectionString, string con)
{
this.connString = connectionString;
this.containter = con;
}
private CloudBlobContainer GetContainer()
{
return CloudStorageAccount
.Parse(this.connString)
.CreateCloudBlobClient()
.GetContainerReference(containter);
}
}
If you know of any other good ways to do this please let me know.


{ 2 comments… read them below or add one }
Exactly what I was looking for. Thank you!
Glad that it helped you out.