s3-mp-cleanup.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. import argparse
  3. import urlparse
  4. import boto
  5. import sys
  6. parser = argparse.ArgumentParser(description="View or remove incomplete S3 multipart uploads",
  7. prog="s3-mp-cleanup")
  8. parser.add_argument("uri", type=str, help="The S3 URI to operate on")
  9. parser.add_argument("-c", "--cancel", help="Upload ID to cancel", type=str, required=False)
  10. def main(uri, cancel):
  11. # Check that dest is a valid S3 url
  12. split_rs = urlparse.urlsplit(uri)
  13. if split_rs.scheme != "s3":
  14. raise ValueError("'%s' is not an S3 url" % uri)
  15. s3 = boto.connect_s3()
  16. bucket = s3.lookup(split_rs.netloc)
  17. mpul = bucket.list_multipart_uploads()
  18. for mpu in mpul:
  19. if not cancel:
  20. print('s3-mp-cleanup.py s3://{}/{} -c {} # {} {}'.format(mpu.bucket.name, mpu.key_name, mpu.id, mpu.initiator.display_name, mpu.initiated))
  21. elif cancel == mpu.id:
  22. bucket.cancel_multipart_upload(mpu.key_name, mpu.id)
  23. break
  24. else:
  25. if cancel:
  26. print("No multipart upload {} found for {}".format(cancel, uri))
  27. sys.exit(1)
  28. if __name__ == "__main__":
  29. args = parser.parse_args()
  30. arg_dict = vars(args)
  31. main(**arg_dict)