Snippets

Mihamina Rakotomandimby Azure CLI: list blobs with pagination

Created by Mihamina Rakotomandimby
#!/bin/bash

rehydrateBlob() {
    if [ "$6" == "Archive" ]
    then
        # prepend a timestamp to the message
        echo "$(date) - Rehydrating $1 > $2 > $3 > $4"
        az storage blob set-tier --account-name $2 --account-key $5 --container-name $3 --name $4 --tier Hot 
        echo "$(date) - Finished rehydrating $1 > $2 > $3 > $4"
    else
        echo "$(date) - $1 > $2 > $3 > $4 is not in archive tier"
    fi
}

loopOnBlobList() {
    sed -i 's/[[:blank:]]\+/,/g' /tmp/bloblist.txt
    while IFS=, read -r blob tier
    do
        rehydrateBlob $1 $2 $3 $blob $4 $tier
    done < /tmp/bloblist.txt
}

storageAccount="storageaccountname"
resourceGroup="resourcegroupname"
storageAccountKey=$(az storage account keys list --account-name $storageAccount --resource-group $resourceGroup --query "[0].value" -o tsv)
containerList=$(az storage container list --account-name $storageAccount --account-key $storageAccountKey --query "[].name" -o tsv)

rm -fv /tmp/bloblist.txt

for container in $containerList
do
    continuationToken=$(az storage blob list --account-name $storageAccount --account-key $storageAccountKey --container-name $container  --query "[].{name:name, tier:properties.blobTier}" -o tsv  2>&1 > /tmp/bloblist.txt | sed 's/ //' |sed -n 2,2p | cut -d ':' -f 2)
    if [[ $continuationToken == "" ]]
    then
        loopOnBlobList $resourceGroup $storageAccount $container $storageAccountKey
    else
        while [[ $continuationToken != "" ]]
        do
            continuationToken=$(az storage blob list --account-name $storageAccount --account-key $storageAccountKey --container-name $container --marker $continuationToken  --query "[].{name:name, tier:properties.blobTier}" -o tsv  2>&1 >> /tmp/bloblist.txt | sed 's/ //' |sed -n 2,2p | cut -d ':' -f 2)
        done
        loopOnBlobList $resourceGroup $storageAccount $container $storageAccountKey
    fi
done

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.