簡體   English   中英

如何檢查 Azure blob 是否存在於多個容器的存儲帳戶中的任何位置?

[英]How to check if Azure blob exists anywhere in storage account in multiple containers?

我有一個 Azure 存儲帳戶,其中有多個容器。 如何檢查所有容器以查看其中是否有特定命名的 blob? blob 也有多個目錄。
我知道有az storage blob exists命令,但這需要容器名稱參數。 我必須先使用 List Containers 命令嗎?

是的,您需要獲取容器列表。 我已經在我的環境中復制並獲得如下預期結果,並且我遵循了Microsoft-Document

其中一種方法是首先,我執行了以下代碼來獲取容器列表:

$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 = 存儲賬戶的密鑰

現在獲取存儲帳戶中所有容器中的每個 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

在此處輸入圖像描述

現在檢查給定的 blob 是否存在,如下所示:

$s="Check blob name"
 if($Target -contains $s){
Write-Host("Blob Exists")
}else{
 Write-Host("Blob Not Exists")
 }
 

在此處輸入圖像描述

或者您可以在獲取容器列表后直接使用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"
} 

在此處輸入圖像描述

是的,您將需要使用 List Containers 命令獲取存儲帳戶中所有容器的列表,然后您可以遍歷容器列表並檢查每個容器是否有您要查找的特定 blob。

下面是一個示例,說明如何使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM