How to create folder structure inside Azure Storage Container using PowerShell
Asked Answered
D

3

0

I have created the storage account along container in Azure using ARM templates. But I want to create folder structure inside the container using PowerShell script.

For example:

Folder1>SubFolder1>

Folder2>SUbFolder2>

…… etc

So, can anyone pls suggest me on this.

Drypoint answered 24/9, 2020 at 15:27 Comment(0)
C
3

There are no directories inside Azure Storage Container. You can create a single container, and then, blobs inside of it.

Alternatively, you can use to include '/' in the blob name.

Eg:

account/container/2020/09/24/sample.txt

where "2020/09/24/sample.txt" is the blobname

Correspondent answered 24/9, 2020 at 16:4 Comment(0)
D
0

You can't create the directories inside the Container All blobs must reside in a blob container, which is simply a logical grouping of blobs. An account can contain an unlimited number of containers, and each container can store an unlimited number of blobs. You can include the / in the container name ("folder/1.txt"). You can create a folder structure

This SO thread may help you in your scenario
Creating an Azure Blob Hierarchy

Duralumin answered 24/9, 2020 at 16:20 Comment(0)
D
-1

You can create the folder structure using the below PowerShell Script.

#connecting to Storage account
$storageAccount = Get-AzStorageAccount -ResourceGroupName "<ResourceGroupName>" 
-AccountName "<storage account>"
$ctx = $storageAccount.Context
# Passing Container name
$filesystemName = "<Container>"
# directory  to be created
$folders1= @('folder1/folder2/folder3','folder4/folder5/folder6')
$FolderArray = ""
for ($i = 0; $i -le ($folders1.length - 1); $i += 1){
    $dirname =""
    $path =""
    $filter=""
    $FolderArray =$folders1[$i].Split("/")
    for ($j = 0; $j -le ($FolderArray.length - 1); $j += 1){
        $dirname = $dirname+$FolderArray[$j]+"/"
        #print Directory name
        $dirname
        $path = $path + $(if ($j -eq 0) {"/"} else { "" })
        $filter =  $filter + $(if ($j -eq 0) {""} else { "/" })+ $FolderArray[$j]
        # Check the directory is exist or not 
        $present = Get-AzDataLakeGen2ChildItem -Context 
        $ctx -FileSystem $filesystemName -Path $path | 
        Where-Object {$_.Name -eq "$($filter)"} -ErrorAction SilentlyContinue
        # Create directory
        if (! $present)
        {
            New-AzDataLakeGen2Item -Context $ctx -FileSystem 
           $filesystemName -Path $dirname -Directory
            Write-Host "The directory $dirname is created"
        }
        # Show the existing folder
        else
        {
            Write-Host "Folder named  $dirname  already exists"
            Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem 
            $filesystemName -Path $path
        }
        $path = $(if ($path -eq "/"){$FolderArray[$j]} else {""}) + 
        $(if ($path -ne "/"){$path +"/"} else {""}) +
        $(if ($path -ne "/"){$FolderArray[$j]} else {""})
        
    }
}
Discoloration answered 31/12, 2020 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.