I have a script that I want to use for Azure to return the resource group, storage account name, storage container name, name of the blob object, size of the blob object, and storage tier.
I have the following:
#!/bin/bash
resourceGroup=$1
storageAccount=$2
containers="$(az storage container list --account-name $2 --auth-mode login --query "[].[name]" -o tsv)"
for c in $containers
do
blob_info=$(az storage blob list --account-name $2 \
--auth-mode login --container-name ${c} \
--query "[].[container,name,properties.contentLength,properties.blobTier]"
-o tsv)
echo "$resourceGroup,$storageAccount,$blob_info" | tr '\t' ','
done
When there are multiple blob items in the JSON I expect the output to show the resource group and storage account along with the complete blob_info for every blob item like this:
$resourceGroup,$storageAccount,$blob_info
$resourceGroup,$storageAccount,$blob_info
However when there are 2 blob items in one JSON the output looks like this (it doesn't echo out the $resourceGroup,$storageAccount for the second blob item in that same JSON output).
$resourceGroup,$storageAccount,$blob_info
$blob_info
I'm thinking it's something simple I must be missing here.
Something like this should be more correct:
#!/bin/bash
resourceGroup=$1
storageAccount=$2
IFS=$'\t\n' read -d '' -r -a containers < <(
az storage container list \
--auth-mode login \
--account-name "$storageAccount" \
--query '[].[name]' \
-o tsv
)
# unescape TSV values
for i in "${!containers[@]}"
do
printf -v 'containers[i]' %b "${containers[i]}"
done
for container in "${containers[@]}"
do
while IFS='' read -r blob_info
do
printf '%b\n' "$(tr '\t' ',' <<< "$resourceGroup,$storageAccount,$blob_info")"
done < <(
az storage blob list \
--auth-mode login \
--account-name "$storageAccount" \
--container-name "$container" \
--query '[].[container,name,properties.contentLength,properties.blobTier]' \
-o tsv
)
done