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
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.
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
To get the parent folders only, you can use the script below.
In my environment, I have a folder structure like this:
Portal:
Script:
To get only parent folders like example, sample, and test from Azure Blob Storage.
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:
Reference:
Get-AzStorageBlob (Az.Storage) | Microsoft Learn