How can I get a list of directories in my container?
I can use Get-AzureStorageBlob
to get all the blobs and filter by distinct prefix /name/, but it might be slow with millions of blobs.
Is there a proper way of achieving this in PowerShell?
There's no concept of directories, only containers and blobs. A blob name may have delimiters with look like directories, and may be filtered.
If you choose to store millions of blobs in a container, then you'll be searching through millions of blob names, even with delimiter filtering, whether using PowerShell, SDK, or direct REST calls.
As far as "proper" way: There is no proper way: Only you can decide how you organize your containers and blobs, and where (or if) you choose to store metadata for more efficient searching (such as a database).
The other answer is correct that there is nothing out of the box as there is no real thing as a folder however only file names that contain a folder like path.
Using a regex in PowerShell you can find top-level folders. As mentioned, this may be slow is there are millions of items in your account but for a small number it may work for you.
$context = New-AzureStorageContext -ConnectionString '[XXXXX]'
$containerName = '[XXXXX]'
$blobs = Get-AzureStorageBlob -Container $containerName -Context $context
$folders = New-Object System.Collections.Generic.List[System.Object]
foreach ($blob in $blobs)
{
if($blob.Name -match '^[^\/]*\/[^\/]*$')
{
$folder = $blob.Name.Substring(0,$blob.Name.IndexOf("/"));
if(!$folders.Contains($folder))
{
$folders.Add($folder)
}
}
}
foreach ($folder in $folders)
{
Write-Host $folder
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With