Last active
March 13, 2017 20:26
-
-
Save odra/8c41baf5a04e207ab3d4f8039ea40098 to your computer and use it in GitHub Desktop.
s3 uploader
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
#!/usr/bin/env python | |
#./upload.py --key KEY --secret SECRET --bucket BUCKET --path /path/to/file --file /absolute-path-to-upload --mimetype image/png --acl public-read | |
import argparse | |
import sys | |
import boto | |
parser = argparse.ArgumentParser(description='uploads file to s3') | |
parser.add_argument('--key', '-k', type=str, required=True, help='s3 auth key') | |
parser.add_argument('--secret', '-s', type=str, required=True, help='s3 auth secret') | |
parser.add_argument('--bucket', '-b', type=str, required=True, help='s3 bucket name') | |
parser.add_argument('--path', '-p', type=str, required=True, help='s3 bucket path to be used') | |
parser.add_argument('--file', '-f', type=str, required=True, help='absolute file path to be uploaded') | |
parser.add_argument('--mimetype', '-m', type=str, default='text/plain', help='file mimetype, default is text/plain') | |
parser.add_argument('--acl', '-a', type=str, default='public-read', help='s3 file acl, default is public-read') | |
client = None | |
def connect(key, secret): | |
global client | |
client = boto.connect_s3(key, secret) | |
def upload(bucket, path, fpath, mimetype='text/plain', acl='public-read'): | |
bucket = client.create_bucket(bucket) | |
key = boto.s3.key.Key(bucket) | |
key.key = path | |
key.metadata = { | |
'Content-Type': mimetype | |
} | |
key.set_contents_from_file(open(fpath)) | |
key.set_acl(acl) | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
connect(args.key, args.secret) | |
try: | |
upload(args.bucket, args.path, args.file, args.mimetype, args.acl) | |
except boto.exception.S3ResponseError as err: | |
print err.message | |
sys.exit(1) | |
print 'https://%s/%s/%s' % (client.server_name(), args.bucket, args.path) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment