Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List directories in a container

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?

like image 635
Stefano d'Antonio Avatar asked Jun 09 '16 10:06

Stefano d'Antonio


2 Answers

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

like image 109
David Makogon Avatar answered Oct 07 '22 18:10

David Makogon


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
}
like image 23
John Avatar answered Oct 07 '22 18:10

John