skip to Main Content

I have an Azure container, this container contains folders (that also contain subfolders and files) and files. I just want the "parent folders" so just the folders i see when i open my container however i cant get it right using $blobs = Get-AzStorageBlob. Because when I loop over blobs it gives me everything in the container while i just want folder and not folder/subfolder/file or folder/file

Hopefully someone can help me out. Thanks in advance.

Kind regards

$blobs = Get-AzStorageBlob -Context $context -Container $container.Name
foreach ($blob in $blobs) {
     write-host $blob.Name
}

2

Answers


  1. Azure Blob Storage has this concept of virtual folders. They are not real folders, just path elements. So something like the below PowerShell should do.

    $blobs = Get-AzStorageBlob -Context $context -Container $container.Name -Blob */*
    foreach ($blob in $blobs) {
        [array]$foldernames += ($blob.name).Split('/')[0]
    }
    
    $foldernames | Select-Object -Unique | Write-Output
    
    #This may be better from an efficiecy/more like PowerShell perspective
    Get-AzStorageBlob -Context $context -Container $container.Name -Blob */* | 
    ForEach-Object {($_.Name).Split('/')[0]} | Select-Object -Unique
    

    This link below mentions ADLS Gen2, which you may want if you need more "real" folders.
    https://learn.microsoft.com/en-us/answers/questions/1306036/controlling-access-to-files-using-virtual-director

    This is the best reference I could find for the concept of virtual folders. If you made yours with Storage Explorer you probably have the "/" character as the delimiter, but it looks like you could have anything.
    https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list#flat-listing-versus-hierarchical-listing

    Login or Signup to reply.
  2. When looping over blobs, it gives me everything in the container, while I just want the folder and not folder/subfolder/file or folder/file.

    To get the parent folders only, you can use the script below.

    In my environment, I have a folder structure like this:

    Portal:

    enter image description here

    Script:

    To get only parent folders like example, sample, and test from Azure Blob Storage.

    Connect-AzAccount
    $resourceGroupName = "zzzz"
    $storageAccountName = "venkat123"
    $containerName = "test"
    
    $context = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
    $blobs = Get-AzStorageBlob -Context $context -Container $containerName
    $folders = $blobs | ForEach-Object {
        $blobName = $_.Name
        if ($blobName.EndsWith("/")) {
            $blobName
        } else {
            $folderName = $blobName.Substring(0, $blobName.LastIndexOf("/") + 1)
            $folderName = $folderName.TrimEnd("/")
        }
        $folderName
    }
    $uniqueFolders = $folders | Select-Object -Unique
    $uniqueFolders
    

    The above script uses Get-AzStorageBlob cmdlet to retrieve all blobs in an Azure container, and then extracts the folder names from the blob paths. It removes any trailing slashes from the folder names, gets the unique folder names, and outputs them to the console, listing only the parent folders in a container.

    Output:

    example
    sample
    test
    

    enter image description here

    Reference:
    Get-AzStorageBlob (Az.Storage) | Microsoft Learn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search