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

14

162

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

Phalange answered 11/4, 2010 at 22:52 Comment(1)
The term for this now is "virtual directory" and you can learn about it here: learn.microsoft.com/en-us/rest/api/storageservices/…Alonzoaloof
W
198

To add on to what Egon said, simply create your blob called "folder/1.txt", and it will work. No need to create a directory.

Willtrude answered 12/4, 2010 at 18:34 Comment(6)
how you filter or get all these files from "folder" ?Forrester
This does not work for me, says containers cant use anything but lowercase, hyphens, numbers. Same for filenamesWhelk
@Whelk you have to use only lowercase letters and numbers for naming your container/directories and that's why you got errors.Moorish
it creates several directories with the same folder, I mean each time I upload file, it create directory called "folder" again , is there any method to check if folder exist so it shouldn't create it?Winwaloe
@SapanGhafuri this is not true, you can create directories containing upper case charactersClairvoyance
When uploading through the Azure UI / gets converted to : so this doesn't seem to workZirkle
S
57

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.

Saleem answered 11/4, 2010 at 23:21 Comment(1)
Can you share C# sample? blob.The name is read only property so we are not able to create a blob.Name with "/"Sapele
A
41

In Azure Portal we have below option while uploading file :

enter image description here

Assessor answered 8/11, 2017 at 6:32 Comment(0)
T
39

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

Thecla answered 4/1, 2016 at 23:43 Comment(3)
This should be the answer as of today's date. +1Leipzig
It's good info but does it answer 'How to create sub directory in a blob container'?Clairvoyance
Warning: this is for obsolete NuGet package...Hahn
B
11

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

Brittaneybrittani answered 15/5, 2017 at 9:40 Comment(0)
D
9

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
Dna answered 22/6, 2019 at 19:9 Comment(0)
C
6

For someone struggling with dynamic directories
As per Version 12

<PackageReference Include="Azure.Storage.Blobs" Version="12.10.0"/>

You can easily have the directory or folder paths separated by a backslash. They will be created automatically in this case. Example:

public async Task UploadFile(string env, string filePath, string filename, Guid companyId, Guid assetId, string baseKey)
{
    var blobContainer = blobServiceClient.GetBlobContainerClient("graphs-data");
    if (!blobContainer.Exists())
    {
      blobContainer.Create();
    }
    var blobClient = blobContainer.GetBlobClient($"input/{env}/{companyId}/iotasset/{assetId}/{baseKey}/{filename}");
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
      await blobClient.UploadAsync(fs, overwrite: true);
}

The results views in Azure storage explorer The four text files uploaded

Cowbind answered 1/3, 2022 at 12:32 Comment(1)
Super simple when using the latest version!Halfon
D
4

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.

Delainedelainey answered 3/12, 2020 at 13:37 Comment(0)
P
3

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).

Pamplona answered 6/6, 2018 at 8:49 Comment(0)
W
1

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
Wachtel answered 1/7, 2018 at 4:20 Comment(0)
H
0

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

Hackler answered 17/2, 2021 at 22:24 Comment(0)
S
0

There is no direct option to create a folder/directory. But if you wish to upload something to folder then while uploading file you need to pass folder name under advance section. For Example If I want to upload a image to an asset named folder then my upload window will look like this.enter image description here

This will create a folder names asset and will upload file to that folder. And point to be noted is folder name and file name are case sensitive.

Salba answered 7/10, 2021 at 12:13 Comment(0)
C
0

As @Egon mentioned,

If you are uploading through azure portal:

click upload > advanced > Upload to folder e.g dir1/dir2/dir3

check this: https://i.stack.imgur.com/Aq5OQ.png

Congresswoman answered 11/3 at 2:53 Comment(1)
This seems duplicative of the other answersStupendous
C
-5

C# instead of accepted English above:

CloudBlobContainer container = new CloudBlobContainer(new Uri(sasUri));
CloudBlockBlob blob = container.GetBlockBlobReference(filePathInSyncFolder);
LocalFileSysAccess.LocalFileSys uploadfromFile = new LocalFileSysAccess.LocalFileSys();
uploadfromFile.uploadfromFilesystem(blob, localFilePath, eventType);

In my opinion, it's simpler 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
Cheri answered 19/2, 2016 at 14:53 Comment(2)
The question is tagged as C#Kino
@Dementic Hence my mention of CoffeeScript, which is just JavaScript without the cruft. Shouldn't be too hard to port to C#, especially compared to the accepted answer that is not even in a programming language.Cheri

© 2022 - 2024 — McMap. All rights reserved.