4
votes

I'm using Microsoft Azure CLI and I could not find a way to list the blob object of a storage account.

I have the list displayed in Azure Web portal but I don't find any away to do it with the az storage command.

I've tried az storage blob list but it required a container name which I don't know how to find it (with az cli).

Do someone have an idea ?

3

3 Answers

8
votes

Update: fetch the account key in cli:

Please try the code below, which can list all the blobs in all the containers of your storage account.

Note that there are no whitespaces aroud "=".

 # list storage account names
 az storage account list --query "[].{name:name}" --output tsv)"

 # update with your storage account name
 storage_account_name="your_storage_account_name"

 key="$(az storage account keys list -n ${storage_account_name} --query "[0].{value:value}" --output tsv)"
    
 containers="$(az storage container list --account-name ${storage_account_name} --account-key $key --query "[].{name:name}" --output tsv)"
    
 for c in $containers
 do
   echo "==== Blobs in Container $c ===="
   az storage blob list --container-name $c \
      --account-name ${storage_account_name} \
      --account-key $key \
      --query "[].{name:name}" --output tsv
 done

Test results as below:

enter image description here

1
votes

Command to get list of containers in a storage account on basis of storage ACCOUNT_NAME and ACCESS_KEY:

az storage container list --account-key ACCESS_KEY --account-name ACCOUNT_NAME

On basis of Container names received in its response, you can use az storage blob list command to get the list of objects within that container.

0
votes

Here is a simple script to list all your blob containers. As a bonus, list all your file shares too !

# List all the storage accounts under a subscription


 for actname in $( az storage account list --query "[].name" --output tsv );  
 do   

# Get the storage key

    key1=`az storage account keys list --account-name $actname --query "[0].value" --output tsv 2>/dev/zero` 

# Exclude the listing when you do not have permission to look into the storage account - like databricks managed storage for example

    if [ $? -ne 0 ] ; then
       continue 
    fi
 
 
    echo -e "\n === Here are the storage containers for your storage account $actname === \n"

# List of containers and file shares for the account

    az storage container list --account-name  $actname --account-key $key1  --query "[].name" --output tsv

    echo -e "\n --- Here are the storage file shares for your storage account $actname ---\n"


    az storage share list --account-name  $actname --account-key $key1 --query "[].name" --output tsv
    
 done