Azure Storage Client v1.0

The November 2009 v1.0 release of theWindows Azure SDK and Tools introduced a completely revamped StorageClient API which is now an integral part of the Azure release instead of being just a sample as before.

Microsoft.WindowsAzure.StorageClient.dll

The storage client functionality is contained in Microsoft.WindowsAzure.StorageClient.dll. This functionality is exposed through about 70 classes spread over 3 namespaces:

The Microsoft.WindowsAzure namespace provides storage account functionality. The Microsoft.WindowsAzure.StorageClient namespace provides the key storage client functionality. The Microsoft.WindowsAzure.StorageClient.Protocol class appears to provide low-level functionality related to the actual REST calls to the Azure Storage Service and is not covered in this post.

Microsoft.WindowsAzure Namespace

The Microsoft.WindowsAzure namespace comprises classes related to storage accounts and credentials:

CloudStorageAccount exposes account and key information and is able to parse the configuration file to retrieve this information. StorageCredentials is an abstract base class for StorageCredentialsAccountAndKey, supporting shared key credentials,  and StorageCredentialsSharedAccessSignature used to create shared access signatures for blobs.

The CloudStorageAccount class is declared:

public sealed class CloudStorageAccount {
    // Constructors
    public CloudStorageAccount(StorageCredentialsAccountAndKey storageCredentialsAccountAndKey, Boolean useHttps);
    public CloudStorageAccount(StorageCredentials storageCredentials, Uri blobEndpoint, Uri queueEndpoint, Uri tableEndpoint);

    // Properties
    public Uri BlobEndpoint { get; }
    public StorageCredentials Credentials { get; }
    public static CloudStorageAccount DevelopmentStorageAccount { get; }
    public Uri QueueEndpoint { get; }
    public Uri TableEndpoint { get; }

    // Methods
    public static CloudStorageAccount FromConfigurationSetting(String settingName);
    public static CloudStorageAccount Parse(String value);
    public static void SetConfigurationSettingPublisher(Action<String,Func<String,Boolean>> configurationSettingPublisher);
    public String ToString(Boolean exportSecrets);
    public static Boolean TryParse(String value, out CloudStorageAccount account);

    // Implemented Interfaces and Overridden Members
    public override String ToString();
}

The CloudStorageAccount class is essential to the storage client since any attempt to use the Azure Storage Service starts by creating an instance of it. Windows Azure services uses the configuration file to store account information such as names and key. It looks as if a simple call to the FromConfigurationSetting() static method would be sufficient to create a CloudStorageAccount object correctly initialized from the configuration file. However, an attempt to do this leads to a runtime error because no Configuration Setting Publisher has been specified. The solution to this is to invoke the SetConfigurationSettingPublisher() method in the (new) OnStart() method for the role. The code required to do this is sufficiently gnarly that the simplest technique appears to be to copy it directly from the thumbnails sample provided with the Azure SDK v1.0.

It is possible to forgo the configuration file and create a CloudStorageAccount object from a StorageCredentialsAccountAndKey object as follows:

StorageCredentialsAccountAndKey storageCredentialsAccountAndKey = new StorageCredentialsAccountAndKey("ACCOUNT_NAME",
           "ACCOUNT_KEY");
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentialsAccountAndKey, true);

This causes the storage client to go directly against cloud storage using the specified account. Note that replacing the ACCOUNT_NAME and ACCOUNT_KEY with those for development storage does not cause development storage to be used and does cause a runtime error. However, the DevelopmentStorageAccount property is a pre-instantiated CloudStorageAccount object that can be used to access development storage:

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

If you do not need the functionality provided by SetConfigurationSettingPublisher() you can get a CloudStorageAccount object using Parse(), as follows:

CloudStorageAccount cloudStorageAccount =
    CloudStorageAccount.Parse( RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));

Note that Adam Sampson has a great post on his Partly Cloudy blog explaining data connection strings for Azure Storage.

Microsoft.WindowsAzure.StorageClient Namespace

This namespace is the core of the storage client and comprises almost 40 classes supporting the full range of functionality for blobs, tables and queues. The classes are listed here along with a short summary of what each of them can be used for. Nearly all functionality is provided in both synchronous and asynchronous form. For example, the CloudQueue class supports the synchronous and asynchronous adding of messages to a queue through the following methods:

public void AddMessage(CloudQueueMessage message, TimeSpan timeToLive);
public void AddMessage(CloudQueueMessage message);
public IAsyncResult BeginAddMessage(CloudQueueMessage message, TimeSpan timeToLive, AsyncCallback callback, Object state);
public IAsyncResult BeginAddMessage(CloudQueueMessage message, AsyncCallback callback, Object state);
public void EndAddMessage(IAsyncResult asyncResult);

UPDATE 12/31/2009

Added link to a Partly Cloudy blog post on data connection strings.

UPDATE 5/6/2010

Added information on CloudStorageAccount.Parse().

About these ads

About Neil Mackenzie

Azure Architect at Satory Global.
This entry was posted in Storage Service, Windows Azure. Bookmark the permalink.

One Response to Azure Storage Client v1.0

  1. Pingback: Yet another application to handle Windows Azure Storage Services - Paolo Salvatori's Blog - Site Home - MSDN Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s