Search code examples
azurecommand-line-interface

Get list of files from az blob container, then grab all


Using the az cli within an azvm, I want to be able to list all the files within a specific container blob and then download those files. Let's say I have a container 'top' and then it has several subdirectories such as:

top/sub1/sub2/sub3/

Where in the folder sub3, there are multiple files. I would like to:

  1. List all the files in sub3, and then

  2. Retrieve all of the files in sub3 through the az cli.

I can only list the contents within 'top' with the command:

az storage container list --account-key ${key} --account-name top --query "[].{Name:name}" --output table

Solution

  • az storage container list --> is used to list the containers inside a storage account.

    Based on the shared information, we have understood that you are trying to list the blob under the subdirectories(sub1/sub2/sub3) inside a contianer top then you need to use az storage blob directory list cmdlet which is used to List blobs and blob subdirectories in a storage directory.

    To test this we have created a storage account with one container (top) and having multiple subdirectories(sub1/sub2/sub3) and we have used the below cmdlet to list the blob inside them.

    az storage blob directory list -c top -d sub1/sub2/sub3 --account-name <storageAccountName> --query [].name -o table
    

    Here is the sample output for reference:

    enter image description here

    Reference documentation for more information about az storage blob directory list cmdlet.