简体   繁体   中英

How to create folder structure inside Azure Storage Container using PowerShell

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.

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

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

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 {""})
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM