-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty_bucket.sh
executable file
·38 lines (29 loc) · 1.14 KB
/
empty_bucket.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
bucket=$1
set -e
echo "Removing all versions from $bucket"
function urldecode {
echo $(python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])" $1);
}
versions=`aws s3api list-object-versions --encoding-type url --bucket $bucket | jq '.Versions'`
markers=`aws s3api list-object-versions --encoding-type url --bucket $bucket | jq '.DeleteMarkers'`
echo "removing files"
for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
version=$(echo ${version} | base64 --decode)
key=`echo $version | jq -r .Key`
versionId=`echo $version | jq -r .VersionId`
decodedVersionId=$(urldecode "$key")
cmd="aws s3api delete-object --bucket $bucket --key $decodedVersionId --version-id $versionId"
echo $cmd
$cmd
done
echo "removing delete markers"
for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
marker=$(echo ${marker} | base64 --decode)
key=`echo $marker | jq -r .Key`
versionId=`echo $marker | jq -r .VersionId`
decodedVersionId=$(urldecode "$key")
cmd="aws s3api delete-object --bucket $bucket --key $decodedVersionId --version-id $versionId"
echo $cmd
$cmd
done