Skip to content

Instantly share code, notes, and snippets.

@concubidated
Created September 27, 2016 22:02
Show Gist options
  • Save concubidated/aaabb187f6c91fe7bd4382bf2f7ab459 to your computer and use it in GitHub Desktop.
Save concubidated/aaabb187f6c91fe7bd4382bf2f7ab459 to your computer and use it in GitHub Desktop.
RGW Multi Delete Boto
import sys, getopt, pprint
import boto.s3
##
## Config Me
##
ACCESS_KEY="ACCESS_KEY_HERE"
SECRET_KEY="SECRET_KEY_HERE"
HOST="S3_HOST_NAME"
BATCH_DELETE_SIZE=1000
##
## End Config
##
def main(argv):
bucketName = ''
channel = ''
runall = ""
try:
opts, args = getopt.getopt(argv,"hab:c::",["bucketName=","channel=","runall="])
except getopt.GetoptError:
print sys.argv[0] +' -b <bucket> -c channel'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print sys.argv[0] + ' -b bucket -c channel'
sys.exit()
elif opt in ("-b", "--bucket"):
bucketName = arg
elif opt in ("-c", "--channel"):
channel = arg
elif opt in ("-a", "--all"):
runall = True
pp = pprint.PrettyPrinter(indent=4)
conn = boto.connect_s3(ACCESS_KEY, SECRET_KEY, host=HOST)
bucket = conn.get_bucket(bucketName)
if not bucketName:
print "Please specify a bucket"
sys.exit(2)
if not channel and not runall:
print "Please specify a channel or use -a for all."
sys.exit(2)
list_of_keys = bucket.list(prefix=channel)
keys_to_delete = []
for key in list_of_keys:
keys_to_delete.append(key)
if len(keys_to_delete) > BATCH_DELETE_SIZE:
result = bucket.delete_keys(keys_to_delete)
pp.pprint(result.__dict__)
keys_to_delete = []
if len(keys_to_delete) > 0:
result = bucket.delete_keys(keys_to_delete)
pp.pprint(result.__dict__)
delete_key_list = []
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