Created
September 12, 2023 20:50
-
-
Save hall757/5e482018fbfde78eee0d1d1fc530436d to your computer and use it in GitHub Desktop.
Remove folder objects from S3 bucket so that when files are removed, the containing folders disappear when empty.
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 | |
# $1 - BUCKET - The S3 bucket to scan | |
# $2 - PATH_TO_CLEAN - The base path to clean. This will | |
# select only subfolders of this path. No leading | |
# or trailing slash. | |
# $3 - DEPTH_TO_KEEP - The number of levels of folders | |
# to exclude from PATH_TO_CLEAN. All folder objects | |
# below this level will be removed. If files still | |
# exist in the folder, the folder will remain visable. | |
# Once files are removed from the removed folder | |
# objects, the infrence will no longer apply and the | |
# folder will vanish. | |
set -o noglob | |
p="$2" | |
if [ $3 -gt 0 ]; then | |
# add a matcher for each subdirectory for DEPTH_TO_KEEP | |
for i in $(seq 1 $3); do | |
p=${p}'/.*?' | |
done | |
fi | |
p=${p#/} # remove leading / | |
if [ "_$2_" != "__" ]||[ $3 -gt 0 ]; then | |
# special case for empty path and zero depth | |
p="$p/" | |
fi | |
# convert glob path into a regex matching files to erase | |
regexp=$(echo "^${p}[^$]" | sed -e 's:/:\\/:g' -e 's/\./\\./g' -e 's/\*/\.\*/g') | |
echo "Scanning $1 for $regexp" | |
aws s3api list-objects --bucket "$1" | | |
grep '"Key":' | # look only for lines containing key | |
sed -e 's/.*"Key": "//' -e 's/[", ]*$//' | # extract just the key | |
grep '/ *$' | # only select objects ending with \ | |
egrep "$regexp" | sort -r | # apply the regex and reverse sort | |
while IFS= read -r line # for each object | |
do # delete it | |
echo -- aws s3api delete-object --bucket "$1" --key "$line" | |
aws s3api delete-object --bucket "$1" --key "$line" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment