Created
August 15, 2017 14:58
-
-
Save AsaK/5d9302f4c167791556889e49a2793960 to your computer and use it in GitHub Desktop.
Script to move local folder to a bucker on AWS S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto | |
| import boto.s3 | |
| import os.path | |
| import sys | |
| AWS_ACCESS_KEY_ID = '' | |
| AWS_ACCESS_KEY_SECRET = '' | |
| bucket_name = 'bucket' | |
| sourceDir = 'SourceDir' | |
| destDir = 'DestDir' | |
| #max size in bytes before uploading in parts. between 1 and 5 GB recommended | |
| MAX_SIZE = 20 * 1000 * 1000 | |
| #size of parts when uploading in parts | |
| PART_SIZE = 6 * 1000 * 1000 | |
| conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY_SECRET) | |
| bucket = conn.create_bucket(bucket_name, | |
| location=boto.s3.connection.Location.DEFAULT) | |
| uploadFileNames = [] | |
| for (sourceDir, dirname, filename) in os.walk(sourceDir): | |
| uploadFileNames.extend(filename) | |
| break | |
| def percent_cb(complete, total): | |
| sys.stdout.write('.') | |
| sys.stdout.flush() | |
| for filename in uploadFileNames: | |
| sourcepath = os.path.join(sourceDir + filename) | |
| destpath = os.path.join(destDir, filename) | |
| print 'Uploading {0} to Amazon S3 bucket {1}'.format(sourcepath, bucket_name) | |
| filesize = os.path.getsize(sourcepath) | |
| if filesize > MAX_SIZE: | |
| print "multipart upload" | |
| mp = bucket.initiate_multipart_upload(destpath) | |
| fp = open(sourcepath,'rb') | |
| fp_num = 0 | |
| while fp.tell() < filesize: | |
| fp_num += 1 | |
| print "uploading part {0}".format(fp_num) | |
| mp.upload_part_from_file(fp, fp_num, cb=percent_cb, num_cb=10, size=PART_SIZE) | |
| mp.complete_upload() | |
| else: | |
| print "singlepart upload" | |
| k = boto.s3.key.Key(bucket) | |
| k.key = destpath | |
| k.set_contents_from_filename(sourcepath, cb=percent_cb, num_cb=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment