Skip to content

Instantly share code, notes, and snippets.

@flavono123
Last active August 3, 2022 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavono123/dcd9a0c28bead3a03d66c9f16abc9563 to your computer and use it in GitHub Desktop.
Save flavono123/dcd9a0c28bead3a03d66c9f16abc9563 to your computer and use it in GitHub Desktop.
Aggregate S3 objects sizes by prefixes
#!/bin/bash
# Remaining prefix after the above prefix should be a format of '%Y/%m'
# ref. https://stackoverflow.com/questions/49382310/iterating-through-min-and-max-dates-in-bash-by-month
cur='2021-07-01'
endepoch=$(date '+%s')
bucket=$1
prefix=$2
while [[ $(date +%s -d "${cur}") -le "${endepoch}" ]]; do
monthprefix=$(date '+%Y/%m' -d "${cur}")
size=$(aws s3api list-objects-v2 --bucket="${bucket}" --prefix="${prefix}/${monthprefix}" | jq '[.Contents[].Size] | add' | numfmt --to=si)
echo "${cur%-*}\t${size}"
cur=$(date '+%Y-%m-%d' -d "$cur + 1 month")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment