Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure file storage - Upload file in nested directories

I want to upload files but in a nested folder environment. I have no problem to create a directory and upload a file but when using nested directories I got a storage exception when trying to create folders. Here is a code example.

   CloudFileDirectory rootDirectory = FileShare.GetRootDirectoryReference();
   if (rootDirectory.Exists())
   {
       cloudFileDirectory = rootDirectory.GetDirectoryReference("Folder/SubFolder"); 

        cloudFileDirectory.CreateIfNotExists(); //Exception occur

        var file = cloudFileDirectory.GetFileReference("File.txt");
   }

Do I have to create a method that create directory for directory or is there a more simple solution?

like image 779
Henke Avatar asked Nov 24 '16 14:11

Henke


People also ask

How do I automatically upload files to Azure blob storage?

Create Power Automate Desktop FlowGo to containers and create a new container. Open the container and on the and navigate to Shared access signature. Select add, create, and write permission, change the time if needed, and press Generate SAS token and URL. Copy the Blob SAS URL and save it as the variable in the flow.

How do I upload a folder in Azure blob storage?

Upload a folder to a blob containerOn the main pane's toolbar, select Upload, and then Upload Folder from the drop-down menu.

How do you store files in Azure blob vs file storage difference?

In summary, the difference between the two storage services is that Azure Blob Storage is a store for objects capable of storing large amounts of unstructured data. On the other hand, Azure File Storage is a distributed, cloud-based file system.


1 Answers

Do I have to create a method that create directory for directory or is there a more simple solution?

Yes, you would need to do that. You can't specify a folder structure and have SDK take care of it for you. Please take a look at sample code below for one approach.

    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();
        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...");
        }
    }
like image 195
Gaurav Mantri Avatar answered Nov 15 '22 05:11

Gaurav Mantri