Wednesday, March 11, 2015

How to get Azure storage Account Key from a CloudStorageAccount object

I have a CloudStorageAccount object and I want to get the AccountKey out of it. Seems like you should be able to get the Cloud Storage Key out of a CloudStorageAccount but I did struggle a bit at first.
I first used the CloudStorageAccount.FromConfigurationSetting(string) method at first and then played about in debug mode to see if I could find it.
I then found that that method doesn't return the correct type of object which left me unable to find my Azure storage access key. I then tried the same thing but using CloudStorageAccount.Parse(string) instead. This did have access to the Azure storage access key.

//this method of getting your CloudStorageAccount is no good here
//var account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionStr");
 
//these two lines do...
var accountVal = RoleEnvironment.GetConfigurationSettingValue("StorageConnectionStr");
var account = CloudStorageAccount.Parse(accountVal);
 
//and then you can retrieve the key like this:
var key = ((StorageCredentialsAccountAndKey)account.Credentials)
          .Credentials.ExportBase64EncodedKey();

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More