Last active
December 18, 2015 12:19
-
-
Save jewer/5782020 to your computer and use it in GitHub Desktop.
command-line python for uploading s3 packages
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.s3 | |
| from boto.s3.key import Key | |
| import sys | |
| import math | |
| def percent_cb(complete, total): | |
| sys.stdout.write(str(math.trunc(complete/max(total, 1)) * 100) + '%..') | |
| if total == complete: | |
| print 'done!' | |
| else: | |
| sys.stdout.flush() | |
| from optparse import OptionParser | |
| parser = OptionParser() | |
| parser.add_option('-i', '--inputfile', dest='input_file', metavar='FILE', help='the file to upload') | |
| parser.add_option('-k', '--key', dest='key_name', metavar='KEY', help='the s3 key to store as') | |
| parser.add_option('-b', '--bucket', dest='bucket_name', metavar='BUCKET', help='the s3 bucket to store in', default='super-awesome-bucket') | |
| parser.add_option('-a', '--aws_access_key', dest='aws_access_key', metavar='KEY', help='the aws access key to authenticate with') | |
| parser.add_option('-s', '--aws_secret_key', dest='aws_secret_key', metavar='KEY', help='the aws secret key to authenticate with') | |
| (options, args) = parser.parse_args() | |
| if options.input_file is None: | |
| print('you need to specify an input file, e.g. -i <filename>') | |
| elif options.key_name is None: | |
| print('you need to specify an key name, e.g. -k <key>') | |
| elif options.bucket_name is None: | |
| print('you need to specify an bucket, or leave out to use default, e.g. -b <bucket>') | |
| elif options.aws_access_key is None: | |
| print('you need to specify an aws_access_key, or leave out to use default, e.g. -b <bucket>') | |
| elif options.aws_secret_key is None: | |
| print('you need to specify an aws_secret_key, e.g. -b <bucket>') | |
| else: | |
| input_file = sys.argv[1:][0] | |
| conn = boto.connect_s3(options.aws_access_key, options.aws_secret_key) | |
| bucket = conn.create_bucket(options.bucket_name) | |
| k = Key(bucket) | |
| k.key = options.key_name | |
| print('About to upload %s to S3 at %s:%s' % (options.input_file, options.bucket_name, options.key_name)) | |
| k.set_contents_from_filename(options.input_file, cb=percent_cb, num_cb=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment