How can I create a sub container in the azure storage location?
6 Answers
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 ]
-
16Please be aware that as of SDK version 2.0 the `GetBlobDirectoryReference` bit doesn't work. Instead, we can use the following: `CloudBlobContainer container = cloudBlobClient.GetContainerReference(directoryName);` `CloudBlobDirectory subDirectory = container.GetDirectoryReference(subDirectoryName);` etc... – Knelis Apr 04 '13 at 12:18
-
3Is there a PHP implementation of this concept? – Vaibhav Kaushal Dec 20 '14 at 11:03
-
Is it supposed to be `cloudBlobClient` or `client` in your function calls? – juunas Oct 24 '16 at 13:42
Are you referring to blob storage? If so, the hierarchy is simply StorageAccount/Container/BlobName. There are no nested containers.
Having said that, you can use slashes in your blob name to simulate nested containers in the URI. See this article on MSDN for naming details.

- 69,407
- 21
- 141
- 189
-
1Thanks a lot, slash in blob name will create nested folders on container for example /a/b.txt – Akash kumar Jul 05 '22 at 11:38
-
@Akashkumar - Not sure I understand the comment but... slashes in a blob name do not create nested folders. Slashes are just part of the file name. For nested folders, you would have to use ADLS (hierarchical storage), which didn't exist in 2010 when I wrote this answer. And non-ADLS blob storage today has no hierarchical folders, and slashes in a filename still result in a flat container/blob structure, as my answer states. – David Makogon Jul 05 '22 at 12:06
I aggree with tobint answer and I want to add something this situation because I also I need the same way upload my games html to Azure Storage with create this directories :
- Games\Beautyshop\index.html
- Games\Beautyshop\assets\apple.png
- Games\Beautyshop\assets\aromas.png
- Games\Beautyshop\customfont.css
- Games\Beautyshop\jquery.js
So After your recommends I tried to upload my content with tool which is Azure Storage Explorer and you can download tool and source code with this url : Azure Storage Explorer
First of all I tried to upload via tool but It doesn't allow to hierarchical directory upload because you don't need : How to create sub directory in a blob container
Finally, I debug Azure Storage Explorer source code and I edited Background_UploadBlobs method and UploadFileList field in StorageAccountViewModel.cs file. You can edit it what you wants.I may have made spelling errors :/ I am so sorry but That's only my recommend.

- 1
- 1

- 309
- 5
- 12
If you are tying to upload files from Azure portal: To create a sub folder in container, while uploading a file you can go to Advanced options and select upload to a folder, which will create a new folder in the container and upload the file into that.

- 970
- 3
- 20
- 46
-
This is a valid solution for some scenarios as it certainly was for mine. If I asked the question, this would have been the chosen answer. – SikoSoft May 07 '20 at 19:01
Kotlin Code
val blobClient = blobContainerClient.getBlobClient("$subDirNameTimeStamp/$fileName$extension");
this will create directory having TimeStamp as name and inside that there will be your Blob File. Notice the use of slash (/) in above code which will nest your blob file by creating folder named as previous string of slash.

- 981
- 3
- 14
- 27
Sample code
string myfolder = "<folderName>";
string myfilename = "<fileName>";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

- 1,304
- 1
- 19
- 31