如何使用Azure CLI获取存储帐户的Blob列表?

19

我正在使用Microsoft Azure CLI,但是我找不到一种列出存储账户 blob 对象的方法。

在Azure Web门户中,我可以看到列表,但是我无法通过az storage命令进行操作。

我尝试了az storage blob list,但它需要一个容器名称,而我不知道如何在az cli中找到它。

有人有思路吗?

5个回答

21

更新: 在cli中获取账户密钥:

请尝试下面的代码,它可以列出存储帐户中所有容器中的所有Blob。

请注意,在“=”周围没有空格。

 # 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

以下为测试结果:

在此输入图片描述


谢谢您的回答,但我该如何在我的程序中获取存储账户密钥? 当我想列出密钥时,我遇到了以下错误。 - skidrow
@skidrow,我更新了代码,在cli代码中获取存储账户密钥。在我的端上运行良好。 - Ivan Glasenberg
当我想使用az cli列出我的帐户存储密钥时,我遇到了以下错误: “客户端'XXX@XXX.onmicrosoft.com'的对象ID为'091bb6b2-XXX-46da-ac31-f350XXXXXX'无权执行操作'Microsoft.Storage/storageAccounts/listKeys/action'在范围内...”。 我知道我没有权限这样做,但是在Web门户中,我的用户帐户能够列出任何存储帐户的blob对象(所有资源=> <storage_account_name> => Objets blob)。 是否可以使用az cli获取此列表(不包括帐户存储密钥)? - skidrow
您可以使用 --auth-mode login 代替帐户密钥,例如 az storage container list --auth-mode login --account-name youraccountname。但是,您需要首先为您的用户设置适当的角色。 - Wojciech Kałuski
@IvanGlasenberg 没有账户密钥就不可能吗?不,先试试 az login - Eat at Joes
显示剩余7条评论

2

命令获取存储帐户的容器列表,基于存储ACCOUNT_NAME和ACCESS_KEY:

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

在其响应中收到的容器名称的基础上,您可以使用az storage blob list 命令获取该容器内对象的列表。


0

感谢Ivan Yang的回答!

由于您的回答所建议的编辑队列已满,因此我想将这个编辑后的代码粘贴回来:

get_blobs.sh

accounts="$(az storage account list --query "[].{name:name}" --output tsv)"
for storage_account_name in $accounts
do
 echo "==== Storage Account ${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

done

我在Azure Cloud CLI中运行了我的脚本:

  1. 从Azure门户的顶部导航启动Cloud Shell。选择Bash。
  2. 使用Unix行结尾保存脚本。将文件拖到Azure CLI上以上传它。如果您遇到带有^r行结尾的错误,请在文件上运行dos2unix get_blobs.sh来修复它。
  3. chmod u+x get_blobs.sh 允许其执行。
  4. ./get_blobs.sh >> output.txt 运行并输出到文本文件。

然后,您可以使用Azure CLI上传/下载按钮Azure CLI Upload/Download Button检索您的output.txt文件。


0

如果我理解正确,解决方案可以分为三个部分:

  1. 获取存储账户列表,并从输出中复制“所需”的账户名称

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

  1. 然后获取“所需”账户的“第一个密钥”,并复制字符串(不要复制双引号:))

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

3- 最后获取所需账户的容器列表(关键在于从第二个命令的输出中正确复制“密钥”)

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

注意- 首先不要忘记登录到 Azure 账户 :)

az login


1
那正是我所需要的。我可以在门户中看到容器,但是任何 azure cli 示例都没有意义,因为我需要一个帐户密钥。 - rob

-1

这里有一个简单的脚本,可以列出所有的 Blob 容器。作为额外的奖励,还可以列出所有的文件共享!

# 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接