skidrow
skidrow

Reputation: 335

How to get the blob list of storage accounts with Azure CLI?

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 ?

Upvotes: 21

Views: 40160

Answers (4)

ajay_full_stack
ajay_full_stack

Reputation: 554

If I understand correctly, the solution can be divided in 3 part

  1. Get the list of storage accounts and copy the "desired" account name from output

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

  1. Then get "first key" of "desired" account and copy the string (dont copy double quotes :) )

az storage account keys list --account-name WHATEVER_ACCOUNTNAME --query "[0].value"

3- Finally get the list of containers of your desired account (catch is here that "key" should be correctly copied from the output of second command)

az storage container list --account-name WHATEVER_ACCOUNTNAME --account-key YOUR-VERY-VERY-LONG-KEY --query "[].{name:name}" --output tsv

Note- Don't forget to login into azure account first of all :).

az login

Upvotes: 2

Sumit S
Sumit S

Reputation: 606

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

Upvotes: -1

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

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

Upvotes: 22

deepak
deepak

Reputation: 131

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.

Upvotes: 2

Related Questions