1
votes

I'm writing a PowerShell script that uses the Azure Storage CLI. I'm looking for a way to list only the directories under a certain path. I can easily list blobs using az storage blob list but I'm hoping I can just get the directories without post-processing the result. Given that Azure blob storage has a flat file structure, I'm not really sure this is possible but thought someone might know something I don't.

Thanks.

3
You mean you're looking at certain path inside the same container ? - Thomas
there is no directory inside a blob container, path are virtual - Thomas

3 Answers

4
votes

You can use the --delimiter option to get closer to what you want -- top level folders (and blobs), but not nested blobs:

# suppress nested blobs
$ az storage blob list -c foo --delimiter '/' --output table 

Name       Blob Type    Content Type             
---------  -----------  ------------------------- 
dir1/
dir2/
dir3/
dir4/
file.txt   BlockBlob    text/plain; charset=utf-8

This suppresses all the blobs at deeper levels. From there, you can filter down to just the "folders" because the folder entries have zero metadata, while the blobs do. The folder entries also end in a /. The az CLI can filter that for you, too, if you provide the query option:

az storage blob list -c foo --delimiter '/' --query '[].name | [?ends_with(@, `/`)]

[ "dir1/",
  "dir2/",
  "dir3/",
  "dir4/" ]
1
votes

As mentioned, Azure Blob Storage is flat. For a hierarchical filesystem, check out Azure File Storage.

Check out the quickstart for the CLI: https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-cli

0
votes

I ended up deciding that this wasn't really an option since the file system is flat and the "paths" are just virtual. I decided to go a different direction with my script that didn't require batching based on these virtual directories.