Skip to content

Instantly share code, notes, and snippets.

@izinin
Created January 19, 2016 10:06
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 izinin/a2ca02ee1ab9f4411a73 to your computer and use it in GitHub Desktop.
Save izinin/a2ca02ee1ab9f4411a73 to your computer and use it in GitHub Desktop.
cleaning AWS S3 with batch
#!/usr/bin/python
import sys, getopt
import subprocess
import string
import pdb
# install AWS cli https://aws.amazon.com/cli/
# usage example :
# python aws_clean.py -p "jongla/broadcast" -b "coverimagedevsg"
def main(argv):
helpMsg = 'aws_clean.py -b <bucket> -p <prefix>'
AWS_LIST = 'aws s3api list-objects --bucket %s --prefix %s --query Contents[*].Key'
AWS_DELETE = 'aws s3 rm s3://%s/%s'
prefix = ''
bucket = ''
try:
opts, args = getopt.getopt(argv,"hp:b:",["prefix="])
except getopt.GetoptError:
print helpMsg
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print helpMsg
sys.exit()
elif opt in ("-p", "--prefix"):
prefix = arg
elif opt in ("-b", "--bucket"):
bucket = arg
if bucket == '' or prefix == '':
print helpMsg
sys.exit()
outlist = subprocess.check_output(string.split(AWS_LIST % (bucket, prefix), ' '))
print outlist
cont = raw_input('\twant to continue ? --> ')
if cont.lower()[0] != 'y':
print '...aborted'
sys.exit()
preplist = map(lambda x: x.strip(), string.split(outlist, '\n'))
flist = map(lambda x: x.strip('",'), filter(lambda x: len(x) > 1 and x[0] == '"', preplist))
for name in flist:
#pdb.set_trace()
print subprocess.check_output(string.split(AWS_DELETE % (bucket, name), ' '), stderr=subprocess.STDOUT)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment