How to create a sub container in azure storage location
Asked Answered
C

6

79

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

Circumbendibus answered 6/7, 2010 at 6:2 Comment(0)
S
135

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 ]

Shepherd answered 6/7, 2010 at 19:1 Comment(3)
Please 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...Rumph
Is there a PHP implementation of this concept?Heredia
Is it supposed to be cloudBlobClient or client in your function calls?Peignoir
B
18

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.

Bellicose answered 6/7, 2010 at 17:16 Comment(2)
Thanks a lot, slash in blob name will create nested folders on container for example /a/b.txtChristophe
@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.Bellicose
P
6

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.

Phalansterian answered 15/1, 2013 at 8:23 Comment(0)
E
5

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.

Elihu answered 5/9, 2019 at 5:7 Comment(1)
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.Guesthouse
C
0

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.

It will look like this on portal enter image description here

Christophe answered 6/7, 2022 at 9:48 Comment(0)
A
-1

Sample code

string myfolder = "<folderName>";
string myfilename = "<fileName>";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
Abaxial answered 8/8, 2017 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.