[英]How to check if Azure blob exists anywhere in storage account in multiple containers?
I have an Azure Storage account and in it are multiple Containers.我有一个 Azure 存储帐户,其中有多个容器。 How can I check all of the Containers to see if a specific named blob is in any of them?
如何检查所有容器以查看其中是否有特定命名的 blob? Also the blobs have multiple directories.
blob 也有多个目录。
I know there's the az storage blob exists
command, but that requires a Container name parameter.我知道有
az storage blob exists
命令,但这需要容器名称参数。 Will I have to use the List Containers command first?我必须先使用 List Containers 命令吗?
Yes, you need to get the containers list.是的,您需要获取容器列表。 I have reproduced in my environment and got expected results as below and I followed Microsoft-Document :
我已经在我的环境中复制并获得如下预期结果,并且我遵循了Microsoft-Document :
One of the way is Firstly, i have executed below code for getting containers list:其中一种方法是首先,我执行了以下代码来获取容器列表:
$storage_account_name="rithemo"
$key="T0M65s8BOi/v/ytQUN+AStFvA7KA=="
$containers=az storage container list --account-name $storage_account_name --account-key $key
$x=$containers | ConvertFrom-json
$x.name
$key = Key of Storage Account
$key = 存储账户的密钥
Now getting every blob present in all containers in my Storage account:现在获取存储帐户中所有容器中的每个 blob:
$Target = @()
foreach($emo in $x.name )
{
$y=az storage blob list -c $emo --account-name $storage_account_name --account-key $key
$y=$y | ConvertFrom-json
$Target += $y.name
}
$Target
Now checking if given blob exists or not as below:现在检查给定的 blob 是否存在,如下所示:
$s="Check blob name"
if($Target -contains $s){
Write-Host("Blob Exists")
}else{
Write-Host("Blob Not Exists")
}
Or you can directly use az storage blob exists
command as below after getting containers list:或者您可以在获取容器列表后直接使用
az storage blob exists
命令,如下所示:
foreach($emo in $x.name )
{
az storage blob exists --account-key $key --account-name $storage_account_name --container-name mycontainer --name $emo --name "xx"
}
Yes, you will need to use the List Containers command to get a list of all the containers in your storage account, and then you can loop through the list of containers and check each one for the specific blob that you are looking for.是的,您将需要使用 List Containers 命令获取存储帐户中所有容器的列表,然后您可以遍历容器列表并检查每个容器是否有您要查找的特定 blob。
Here's an example of how you can accomplish this using the Azure CLI下面是一个示例,说明如何使用 Azure CLI 完成此操作
# First, get a list of all the containers in your storage account
containers=$(az storage container list --account-name YOUR_STORAGE_ACCOUNT_NAME --output tsv --query '[].name')
# Loop through the list of containers
for container in $containers
do
# Check if the specific blob exists in the current container
az storage blob exists --account-name YOUR_STORAGE_ACCOUNT_NAME --container-name $container --name YOUR_BLOB_NAME
# If the blob exists, print a message and break out of the loop
if [ $? -eq 0 ]; then
echo "Blob found in container: $container"
break
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.