How to create a sub container in azure storage location

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 “https://stackoverflow.com/” 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 ]

Leave a Comment