Created
May 24, 2017 09:10
-
-
Save wwwins/48c04dbf4bd8d4e2f1bbe1bebeb43afc to your computer and use it in GitHub Desktop.
python boto3 upload files to 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
# -*- coding: utf-8 -*- | |
""" | |
Usage: | |
python s3upload.py -b bucket -i image.jpg | |
python s3upload.py -b bucket -f images -i image.jpg | |
python s3upload.py -b bucket -f images -i image.jpg -t img.jpg | |
python s3upload.py -p tokyo -b bucket -i image.jpg | |
""" | |
from argparse import ArgumentParser | |
from pprint import pprint | |
import boto3 | |
import os | |
def get_client(profile): | |
boto3.setup_default_session(profile_name=profile) | |
client = boto3.client('s3') | |
return client | |
def get_args(): | |
parser = ArgumentParser(description='upload file to s3') | |
parser.add_argument('-p', '--profile', default='default') | |
parser.add_argument('-i', '--image') | |
parser.add_argument('-b', '--bucket') | |
parser.add_argument('-f', '--folder') | |
parser.add_argument('-t', '--target') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = get_args() | |
# pprint(args) | |
folder = '' | |
image = '' | |
if args.folder: | |
folder = args.folder+'/'; | |
if args.target: | |
image = args.target; | |
else: | |
image = os.path.basename(args.image) | |
# print(args.bucket+'/'+folder+image) | |
client = get_client(args.profile) | |
client.upload_file(args.image, args.bucket, folder+image) | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment