【问题标题】:How to get the blob list of storage accounts with Azure CLI?如何使用 Azure CLI 获取存储帐户的 blob 列表?
【发布时间】:2019-03-21 18:52:30
【问题描述】:

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

我在 Azure Web 门户中显示了该列表,但我找不到任何可以使用 az storage 命令的方法。

我尝试过az storage blob list,但它需要一个我不知道如何找到它的容器名称(使用 az cli)。

有人有想法吗?

【问题讨论】:

    标签: azure azure-blob-storage


    【解决方案1】:

    更新:在 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,为什么不直接从 azure 门户获取密钥?你得到了哪个错误?
    • @skidrow,我更新了代码,它在 cli 代码中获取存储帐户密钥。在我身边工作得很好。
    • 当我想用az cli 列出我的帐户存储密钥时出现以下错误:The client 'XXX@XXX.onmicrosoft.com' with object id '091bb6b2-XXX-46da-ac31-f350XXXXXX' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over scope ...。我知道我无权这样做,但在 Web 门户中,我的用户帐户能够列出任何存储帐户的 blob 对象(所有资源 => => Objets blob)。 az cli 无法获取此列表(没有帐户存储密钥)?
    • @skidrow,是的,没有钥匙,这是不可能的 :(.
    【解决方案2】:

    Command 根据存储 ACCOUNT_NAME 和 ACCESS_KEY 获取存储帐户中的容器列表:

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

    根据在其响应中收到的容器名称,您可以使用 az storage blob list command 获取该容器内的对象列表。

    【讨论】:

      【解决方案3】:

      感谢 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。选择重击。
      2. 以 Unix 行结尾保存脚本。将文件拖到 Azure CLI 上以进行上传。如果您收到 ^r 行结尾错误,请在文件上运行 dos2unix get_blobs.sh 以解决该问题。
      3. chmod u+x get_blobs.sh 允许它执行。
      4. ./get_blobs.sh >> output.txt 运行并输出到文本文件。

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

      【讨论】:

        【解决方案4】:

        这是一个列出所有 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
        

        【讨论】:

          【解决方案5】:

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

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

          az 存储帐户列表 --query "[].{name:name}" --output tsv

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

          az 存储帐户密钥列表 --account-name WHATEVER_ACCOUNTNAME --query "[0].value"

          3- 最后得到你想要的帐户的容器列表(这里要注意的是,“key”应该从第二个命令的输出中正确复制)

          az 存储容器列表 --account-name WHATEVER_ACCOUNTNAME --account-key YOUR-VERY-VERY-LONG-KEY --query "[].{name:name}" --output tsv

          注意-首先不要忘记登录 azure 帐户 :)。

          az 登录

          【讨论】:

            猜你喜欢
            • 2021-07-27
            • 1970-01-01
            • 2014-07-25
            • 2019-12-05
            • 1970-01-01
            • 2017-05-01
            • 2021-02-03
            • 2022-11-11
            • 2015-05-13
            相关资源
            最近更新 更多