[c#] Microsoft Azure: How to create sub directory in a blob container

How to create a sub directory in a blob container

for example,

in my blob container http://veda.blob.core.windows.net/document/

If I store some files it will be

http://veda.blob.core.windows.net/document/1.txt

http://veda.blob.core.windows.net/document/2.txt

Now, how to create a sub directory

http://veda.blob.core.windows.net/document/folder/

So that I can store files

http://veda.blob.core.windows.net/document/folder/1.txt

This question is related to c# azure directory azure-blob-storage

The answer is


There is a comment by @afr0 asking how to filter on folders..

There is two ways using the GetDirectoryReference or looping through a containers blobs and checking the type. The code below is in C#

CloudBlobContainer container = blobClient.GetContainerReference("photos");

//Method 1. grab a folder reference directly from the container
CloudBlobDirectory folder = container.GetDirectoryReference("directoryName");

//Method 2. Loop over container and grab folders.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
    if (item.GetType() == typeof(CloudBlobDirectory))
    {
        // we know this is a sub directory now
        CloudBlobDirectory subFolder = (CloudBlobDirectory)item;

        Console.WriteLine("Directory: {0}", subFolder.Uri);
    }
}

read this for more in depth coverage: http://www.codeproject.com/Articles/297052/Azure-Storage-Blobs-Service-Working-with-Directori


If you use Microsoft Azure Storage Explorer, there is a "New Folder" button that allows you to create a folder in a container. This is actually a virtual folder:

enter image description here


As @Egon mentioned above, there is no real folder management in BLOB storage.

You can achieve some features of a file system using '/' in the file name, but this has many limitations (for example, what happen if you need to rename a "folder"?).

As a general rule, I would keep my files as flat as possible in a container, and have my application manage whatever structure I want to expose to the end users (for example manage a nested folder structure in my database, have a record for each file, referencing the BLOB using container-name and file-name).


There is actually only a single layer of containers. You can virtually create a "file-system" like layered storage, but in reality everything will be in 1 layer, the container in which it is.

For creating a virtual "file-system" like storage, you can have blob names that contain a '/' so that you can do whatever you like with the way you store. Also, the great thing is that you can search for a blob at a virtual level, by giving a partial string, up to a '/'.

These 2 things, adding a '/' to a path and a partial string for search, together create a virtual "file-system" storage.


You do not need to create sub directory. Just create blob container and use file name like the variable filename as below code:

string filename = "document/tech/user-guide.pdf";
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(filename);
blob.StreamWriteSizeInBytes = 20 * 1024;
blob.UploadFromStream(fileStream); // fileStream is System.IO.Stream

Here's how i do it in CoffeeScript on Node.JS:

blobService.createBlockBlobFromText 'containerName', (path + '$$$.$$$'), '', (err, result)->
    if err
        console.log 'failed to create path', err
    else
        console.log 'created path', path, result

In Azure Portal we have below option while uploading file :

enter image description here


Wanted to add the UI portal Way to do as well. In case you want to create the folder structure, you would have to do it with the complete path for each file.

enter image description here

You need to Click on Upload Blob, Expand the Advanced and put it the path saying "Upload to Folder"

So Lets say you have a folder assets you want to upload and the content of the folder look like below

enter image description here

And if you have a folder under js folder with name main.js, you need to type in the path "assets/js" in upload to folder. Now This has to be done for each file. In case you have a lot of file, its recommended you do it programmatically.


I needed to do this from Jenkins pipeline, so, needed to upload files to specific folder name but not to the root container folder. I use --destination-path that can be folder or folder1/folder2

az storage blob upload-batch --account-name $AZURE_STORAGE_ACCOUNT --destination ${CONTAINER_NAME} --destination-path ${VERSION_FOLDER} --source ${BUILD_FOLDER} --account-key $ACCESS_KEY

hope this help to someone


Got similar issue while trying Azure Sample first-serverless-app.
Here is the info of how i resolved by removing \ at front of $web.

Note: $web container was created automatically while enable static website. Never seen $root container anywhere.

//getting Invalid URI error while following tutorial as-is
az storage blob upload-batch -s . -d \$web --account-name firststgaccount01

//Remove "\" @destination param
az storage blob upload-batch -s . -d $web --account-name firststgaccount01

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to azure

How is VIP swapping + CNAMEs better than IP swapping + A records? 'Connect-MsolService' is not recognized as the name of a cmdlet Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" How to view the roles and permissions granted to any database user in Azure SQL server instance? How to get the azure account tenant Id? Azure SQL Database "DTU percentage" metric Could not load file or assembly System.Web.Http.WebHost after published to Azure web site How to re-create database for Entity Framework? How do I create a new user in a SQL Azure database?

Examples related to directory

Moving all files from one directory to another using Python What is the reason for the error message "System cannot find the path specified"? Get folder name of the file in Python How to rename a directory/folder on GitHub website? Change directory in Node.js command prompt Get the directory from a file path in java (android) python: get directory two levels up How to add 'libs' folder in Android Studio? How to create a directory using Ansible Troubleshooting misplaced .git directory (nothing to commit)

Examples related to azure-blob-storage

Microsoft Azure: How to create sub directory in a blob container