簡體   English   中英

Shell 使用 AWS CLI 獲取 S3 存儲桶大小的腳本

[英]Shell script to fetch S3 bucket size with AWS CLI

我有這個腳本可以獲取 AWS 中的所有存儲桶及其大小。 但是當我運行腳本時它會獲取存儲桶,但是當運行循環來獲取大小時,它會拋出錯誤。 有人能指出我哪里出錯了嗎? bcos 當我為單個存儲桶運行 awscli 命令時,它可以毫無問題地獲取大小。 所需的 output 將如下所示,但對於所有桶,我已經提取了一個桶。 期望的輸出:

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

嘗試這個:

#!/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是一個字符串,而不是一個數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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