Get File Count and Storage Size for AWS S3 Buckets

Today I got my AWS bill and while not super high… the one section I noticed that was up there was my S3 cost. At almost $20 and no public buckets other than a few websites that I host for virtually no cost, this seemed high. So here’s a quick bash script using the AWS CLI. Note I’m on Ubuntu Linux if you haven’t figured out by now. Next step is to figure out which domains to keep and which to toss to get my $80 a month Route 53 cost down 😐

aws s3 ls --profile=podcastgs | awk '{print $3}' | while read bucket; do
  echo "Bucket: $bucket"
  output=$(aws s3 ls s3://$bucket --recursive --summarize --profile=podcastgs 2>&1)
  echo "$output" | grep "Total"
  
  # Extract and convert size
  size_bytes=$(echo "$output" | grep "Total Size:" | awk '{print $3}')
  if [ ! -z "$size_bytes" ]; then
    size_mb=$(echo "scale=2; $size_bytes / 1024 / 1024" | bc)
    size_gb=$(echo "scale=2; $size_bytes / 1024 / 1024 / 1024" | bc)
    echo "Size: ${size_mb} MB (${size_gb} GB)"
  fi
  echo ""
done
Author: Miyar Es