s3upload_boto.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. import os, sys
  3. import math
  4. import boto
  5. AWS_ACCESS_KEY_ID = ''
  6. AWS_SECRET_ACCESS_KEY = ''
  7. def upload_file(s3, bucketname, file_path):
  8. b = s3.get_bucket(bucketname)
  9. filename = os.path.basename(file_path)
  10. k = b.new_key(filename)
  11. mp = b.initiate_multipart_upload(filename)
  12. source_size = os.stat(file_path).st_size
  13. bytes_per_chunk = 5000*1024*1024
  14. chunks_count = int(math.ceil(source_size / float(bytes_per_chunk)))
  15. for i in range(chunks_count):
  16. offset = i * bytes_per_chunk
  17. remaining_bytes = source_size - offset
  18. bytes = min([bytes_per_chunk, remaining_bytes])
  19. part_num = i + 1
  20. print( "uploading part " + str(part_num) + " of " + str(chunks_count))
  21. with open(file_path, 'r') as fp:
  22. fp.seek(offset)
  23. mp.upload_part_from_file(fp=fp, part_num=part_num, size=bytes)
  24. if len(mp.get_all_parts()) == chunks_count:
  25. mp.complete_upload()
  26. print ("upload_file done")
  27. else:
  28. mp.cancel_upload()
  29. print ("upload_file failed")
  30. if __name__ == "__main__":
  31. if len(sys.argv) != 3:
  32. print ("usage: python s3upload.py bucketname filepath")
  33. exit(0)
  34. bucketname = sys.argv[1]
  35. filepath = sys.argv[2]
  36. s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  37. upload_file(s3, bucketname, filepath)
  38. #python s3upload.py bucketname extremely_large_file.txt