6
votes

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?

2
Not sure what you mean by 'proper way'. Also note: there are no directories inside a container, only blobs (but with names that might mimic directories though). - David Makogon
What are you trying to do? What is the actual problem you are trying to solve? Cloud containers, whether Azure, Amazon, Google, Openstack have no directories. Directories imply recursion and that doesn't scale. All of them use a character like / as a separator and emulate directory operations over the flat list of files. - Panagiotis Kanavos
Consider that it's actually very fast to get a list of all files under a container, or at least using a high level prefix, filter it using string operations on the client side then requesting the individual files you need. - Panagiotis Kanavos
@DavidMakogon this isn't fully true, I thought the same until I saw this: msdn.microsoft.com/en-gb/library/… that makes me think there is a concept of directory for the blobs. - Stefano d'Antonio
@PanagiotisKanavos it takes 30minutes every time so it's not really an acceptable speed. - Stefano d'Antonio

2 Answers

3
votes

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).

3
votes

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
}