Last active
August 3, 2022 10:11
-
-
Save flavono123/dcd9a0c28bead3a03d66c9f16abc9563 to your computer and use it in GitHub Desktop.
Aggregate S3 objects sizes by prefixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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