Skip to content

Instantly share code, notes, and snippets.

@clarkritchie
Last active March 9, 2024 00:24
Show Gist options
  • Save clarkritchie/fdce6b1a365ce176040bc8e7fca3a0c7 to your computer and use it in GitHub Desktop.
Save clarkritchie/fdce6b1a365ce176040bc8e7fca3a0c7 to your computer and use it in GitHub Desktop.
Quick and dirty script for managing S3 buckets
#!/usr/bin/env bash
# quick and dirty script to
# - list buckets matching
# - copy buckets matching
# - remove buckets matching
SEARCH=${1}
ACTION=${2:-"ls"}
if [ -z ${SEARCH} ]; then
echo "Need search base"
exit 0
fi
buckets=$(aws s3api list-buckets --query 'Buckets[?starts_with(Name, `'${SEARCH}'`) == `true`].Name' --output text)
for b in ${buckets[@]}
do
if [[ "${ACTION}" == "ls" ]]; then
aws s3 ls s3://${b}
elif [[ "${ACTION}" == "cp" ]]; then
aws s3 cp s3://"${b}" "./${b}" --recursive
elif [[ "${ACTION}" == "rm" ]]; then
# if the bucket has versioning enabled, this should work
AWS_PAGER="" aws s3api delete-objects \
--bucket ${b} \
--delete "$(AWS_PAGER="" aws s3api list-object-versions \
--bucket ${b} | \
jq '{Objects: [.Versions[] | {Key:.Key, VersionId : .VersionId}], Quiet: false}')"
aws s3 rb --force s3://${b}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment