Tag Archives: Storage

Azure Storage Client helpers

In my last post, I described how the configuration setting publisher worked. More specifically, I covered how to use the typical use-case of retrieving connection strings from Azure or local configuration and the typical errors you might produce when you use this incorrectly. I found myself writing the same code very often so I encapsulated this into a simple set of fluent-ish extensions (I say “ish” because by technical definition of fluent, these aren’t self-referencing… yet). That said, I thought I’d share this code as it helps me get my apps started pretty quickly.

// Author: Tobin Titus <tobin.titus@gmail.com>
// Copyright (c) 2010 Tobin Titus
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Configuration;

namespace AzureNinja.Storage
{
  public class Client
  {
    public const string DEFAULT_CONNECTION_STRING_NAME
          = "StorageConnectionString";
    static Client()
    {
       CloudStorageAccount.SetConfigurationSettingPublisher(
          (configName, configSettingPublisher) =>
          {
              var connectionString = RoleEnvironment.IsAvailable
                 ? RoleEnvironment.GetConfigurationSettingValue(configName)
                 : ConfigurationManager.AppSettings[configName];
               configSettingPublisher(connectionString);
          });
    }

    public static CloudStorageAccount FromConfig(
       string configSetting = DEFAULT_CONNECTION_STRING_NAME)
    {
        return CloudStorageAccount.FromConfigurationSetting(configSetting);
    }

    public static CloudStorageAccount FromString(string connectionString)
    {
       return CloudStorageAccount.Parse(connectionString);
    }
  }

  public static class CloudStorageClientHelpers
  {
    public static CloudBlobClient ForBlobs(this CloudStorageAccount account)
    {
       return account.CreateCloudBlobClient();
    }
    public static CloudQueueClient ForQueues(this CloudStorageAccount account)
    {
        return account.CreateCloudQueueClient();
    }
    public static CloudTableClient ForTables(this CloudStorageAccount account)
    {
        return account.CreateCloudTableClient();
    }
  }
}

You can then construct your requests for a CloudXxxClient as follows:

CloudBlobClient  client1 = Client.FromConfig().ForBlobs();
CloudQueueClient client2 = Client.FromConfig().ForQueues();
CloudTableClient client3 = Client.FromConfig().ForTables();

This uses “StorageConnectionString” as the default configuration key. And you can specify configuration key as well:

Client.FromConfig(“myConfigKey”).ForBlobs();

Furthermore, this allows you to concentrate on your calls to an otherwise fluent Azure API:

Client.FromConfig().ForBlobs().GetContainerReference(“myContainer”).Delete();

or

Client.FromConfig().ForBlobs()
     .GetContainerReference("myContainer").CreateIfNotExist();

Let me know if you have any questions or input.