Azure File Storage: Create nested directories
Asked Answered
I

3

13

My code looks like this

CloudFileClient client = ...;

client.GetShareReference("fileStorageShare")
    .GetRootDirectoryReference()
    .GetDirectoryReference("one/two/three")
    .Create();

This errors if directories one or two don't exist. Is there a way to create these nested directories with a single call?

Informed answered 1/10, 2018 at 14:30 Comment(4)
If I'm not mistaken, there is no nested directories in azure blob storage, it's a flat hierarchy only, you might be able to build a nested directory just so you can see it in the explorer and nothing more.Hotheaded
@bleh10 You're right for blob storage which is what file storage sits on top of but file storage does have some directory support.Informed
oh I never knew that! Thanks for the info, might get handy. Did you get any new result for your question?Hotheaded
@bleh10 Not yet. I suspect it will have to be a new feature.Informed
C
20

It is impossible. The SDK does not support it this way, you should create them one by one.

A issue has already submitted here.

If you wanna create them one by one, you can use the following sample code:

static void NestedDirectoriesTest()
{
   var cred = new StorageCredentials(accountName, accountKey);
   var account = new CloudStorageAccount(cred, true);
   var client = account.CreateCloudFileClient();
   var share = client.GetShareReference("temp2");
   share.CreateIfNotExists();
   var cloudFileDirectory = share.GetRootDirectoryReference();

   //Specify the nested folder
   var nestedFolderStructure = "Folder/SubFolder";
   var delimiter = new char[] { '/' }; 
   var nestedFolderArray = nestedFolderStructure.Split(delimiter);
   for (var i=0; i<nestedFolderArray.Length; i++)
   {
       cloudFileDirectory = cloudFileDirectory.GetDirectoryReference(nestedFolderArray[i]);
       cloudFileDirectory.CreateIfNotExists();
       Console.WriteLine(cloudFileDirectory.Name + " created...");
   }
}
Caster answered 16/10, 2018 at 5:49 Comment(1)
dumb that they appear to ignore their users' request for basic functionality.Effusive
C
7

Following the advice of Ivan Yang, I adapted my code using Azure.Storage.Files.Shares (Version=12.2.3.0).

Here's my contribution:

readonly string storageConnectionString = "yourConnectionString";
readonly string shareName = "yourShareName";

public string StoreFile(string dirName,string fileName, Stream fileContent)
{
    // Get a reference to a share and then create it
    ShareClient share = new ShareClient(storageConnectionString, shareName);
    share.CreateIfNotExists();

    // Get a reference to a directory and create it
    string[] arrayPath = dirName.Split('/');
    string buildPath = string.Empty;
    var tempoShare = share;
    ShareDirectoryClient directory = null; // share.GetDirectoryClient(dirName);
    // Here's goes the nested directories builder
    for (int i=0; i < arrayPath.Length; i++)
    {
        buildPath += arrayPath[i];
        directory = share.GetDirectoryClient(buildPath);
        directory.CreateIfNotExists();
        buildPath += '/';
    }
     // Get a reference to a file and upload it
    ShareFileClient file = directory.GetFileClient(fileName);
    using (Stream stream = fileContent)
    {
        file.Create(stream.Length);
        file.UploadRange(new HttpRange(0, stream.Length), stream);
    }
    return directory.Path;
}
Cofer answered 16/10, 2020 at 22:8 Comment(0)
O
5

Here is a simplified version of Hagen's code:

public async Task<ShareFileClient> CreateFileClient(string connection, string shareName, string path)
{
    var share = new ShareClient(connection, shareName);
    await share.CreateIfNotExistsAsync();

    var dir = share.GetRootDirectoryClient();
    var pathChain = path.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
    int dirCount = pathChain.Length - 1;

    for (int i = 0; i < dirCount; ++i)
    {
        dir = dir.GetSubdirectoryClient(pathChain[i]);
        await dir.CreateIfNotExistsAsync();
    }

    return dir.GetFileClient(pathChain[dirCount]);
}
October answered 6/10, 2021 at 6:20 Comment(1)
I'm going to test your enhance version of my code. Thanks!Cofer

© 2022 - 2024 — McMap. All rights reserved.