如何检查Azure Blob是否存在于多个容器中存储帐户的任何位置?

zbdgwd5y  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(145)

我有一个Azure存储帐户,其中有多个容器。如何检查所有容器以查看其中是否有特定命名的Blob?而且Blob有多个目录。
我知道有az storage blob exists命令,但它需要一个容器名称参数。我必须先使用列出容器命令吗?

svujldwt

svujldwt1#

是的,你需要得到容器列表。我已经在我的环境中复制,并得到了预期的结果,如下所示,我按照微软文档:
方法之一是首先,我已经执行了下面的代码获取容器列表:

  1. $storage_account_name="rithemo"
  2. $key="T0M65s8BOi/v/ytQUN+AStFvA7KA=="
  3. $containers=az storage container list --account-name $storage_account_name --account-key $key
  4. $x=$containers | ConvertFrom-json
  5. $x.name

x1c 0d1x *$key =存储帐户密钥 *
现在获取我的存储帐户中所有容器中存在的每个blob:

  1. $Target = @()
  2. foreach($emo in $x.name )
  3. {
  4. $y=az storage blob list -c $emo --account-name $storage_account_name --account-key $key
  5. $y=$y | ConvertFrom-json
  6. $Target += $y.name
  7. }
  8. $Target

现在检查给定的blob是否存在,如下所示:

  1. $s="Check blob name"
  2. if($Target -contains $s){
  3. Write-Host("Blob Exists")
  4. }else{
  5. Write-Host("Blob Not Exists")
  6. }

也可以在得到containers列表后直接使用az storage blob exists命令,如下所示:

  1. foreach($emo in $x.name )
  2. {
  3. az storage blob exists --account-key $key --account-name $storage_account_name --container-name mycontainer --name $emo --name "xx"
  4. }

展开查看全部
nwwlzxa7

nwwlzxa72#

是的,您将需要使用List Containers命令来获取存储帐户中所有容器的列表,然后您可以遍历容器列表,并检查每个容器是否有您要查找的特定blob。
下面的示例说明了如何使用Azure CLI完成此操作

  1. # First, get a list of all the containers in your storage account
  2. containers=$(az storage container list --account-name YOUR_STORAGE_ACCOUNT_NAME --output tsv --query '[].name')
  3. # Loop through the list of containers
  4. for container in $containers
  5. do
  6. # Check if the specific blob exists in the current container
  7. az storage blob exists --account-name YOUR_STORAGE_ACCOUNT_NAME --container-name $container --name YOUR_BLOB_NAME
  8. # If the blob exists, print a message and break out of the loop
  9. if [ $? -eq 0 ]; then
  10. echo "Blob found in container: $container"
  11. break
  12. fi
  13. done

相关问题