How to create a sub container in azure storage location

subramani picture subramani · Jul 6, 2010 · Viewed 33.2k times · Source

How can I create a sub container in the azure storage location?

Answer

tobint picture tobint · Jul 6, 2010

Windows Azure doesn't provide the concept of heirarchical containers, but it does provide a mechanism to traverse heirarchy by convention and API. All containers are stored at the same level. You can gain simliar functionality by using naming conventions for your blob names.

For instance, you may create a container named "content" and create blobs with the following names in that container:

content/blue/images/logo.jpg
content/blue/images/icon-start.jpg
content/blue/images/icon-stop.jpg

content/red/images/logo.jpg
content/red/images/icon-start.jpg
content/red/images/icon-stop.jpg

Note that these blobs are a flat list against your "content" container. That said, using the "/" as a conventional delimiter, provides you with the functionality to traverse these in a heirarchical fashion.

protected IEnumerable<IListBlobItem> 
          GetDirectoryList(string directoryName, string subDirectoryName)
{
    CloudStorageAccount account =
        CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    CloudBlobClient client = 
        account.CreateCloudBlobClient();
    CloudBlobDirectory directory = 
        client.GetBlobDirectoryReference(directoryName); 
    CloudBlobDirectory subDirectory = 
        directory.GetSubdirectory(subDirectoryName); 

    return subDirectory.ListBlobs();
}

You can then call this as follows:

GetDirectoryList("content/blue", "images")

Note the use of GetBlobDirectoryReference and GetSubDirectory methods and the CloudBlobDirectory type instead of CloudBlobContainer. These provide the traversal functionality you are likely looking for.

This should help you get started. Let me know if this doesn't answer your question:

[ Thanks to Neil Mackenzie for inspiration ]