我有這個腳本可以獲取 AWS 中的所有存盤桶以及大小。但是當我運行腳本時它會獲取存盤桶,但是當運行回圈以獲取大小時,它會拋出錯誤。有人可以指出我哪里出錯了。bcos 當我為單個存盤桶運行 awscli 命令時,它可以毫無問題地獲取大小。所需的輸出將如下所示,但對于所有存盤桶,我已經提取了一個存盤桶。期望的輸出:
aws --profile aws-stage s3 ls s3://<bucket> --recursive --human-readable --summarize | awk END'{print}'
Total Size: 75.1 KiB
錯誤:
Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]*:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-.]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9] :[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
腳本:
#!/bin/bash
aws_profile=('aws-stage' 'aws-prod');
#loop AWS profiles
for i in "${aws_profile[@]}"; do
echo "${i}"
buckets=$(aws --profile "${i}" s3 ls s3:// --recursive | awk '{print $3}')
#loop S3 buckets
for j in "${buckets[@]}"; do
echo "${j}"
aws --profile "${i}" s3 ls s3://"${j}" --recursive --human-readable --summarize | awk END'{print}'
done
done
uj5u.com熱心網友回復:
試試這個:
#!/bin/bash
aws_profiles=('aws-stage' 'aws-prod');
for profile in "${aws_profiles[@]}"; do
echo "$profile"
read -rd "\n" -a buckets <<< "$(aws --profile "$profile" s3 ls | cut -d " " -f3)"
for bucket in "${buckets[@]}"; do
echo "$bucket"
aws --profile "$profile" s3 ls s3://"$bucket" --human-readable --summarize | awk END'{print}'
done
done
問題是你buckets是一個字串,而不是一個陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/471354.html
上一篇:AWSCLI|esc回傳文本
下一篇:如何使用JQ提取此欄位?
