Last active
May 16, 2018 19:03
-
-
Save derekedelaney/1d46377d0141c9d1496f5a344aec10f2 to your computer and use it in GitHub Desktop.
uploads files and folders to an s3 bucket
This file contains 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
from __future__ import print_function | |
import os | |
import sys | |
import argparse | |
import boto3 | |
from botocore.exceptions import ClientError | |
def upload_to_s3(bucket, artefact, is_folder, bucket_key): | |
""" | |
Uploads an artefact to Amazon S3 | |
""" | |
try: | |
client = boto3.client('s3') | |
except ClientError as err: | |
print("Failed to create boto3 client.\n" + str(err)) | |
return False | |
if is_folder == 'true': | |
for root, dirs, files in os.walk(artefact): | |
for file in files: | |
directory = root.replace(artefact, "") | |
directory = directory.replace("/", "") | |
print(os.path.join(directory, file)) | |
try: | |
client.upload_file(os.path.join(root, file), bucket, os.path.join(directory, file)) | |
except ClientError as err: | |
print("Failed to upload artefact to S3.\n" + str(err)) | |
return False | |
except IOError as err: | |
print("Failed to access artefact in this directory.\n" + str(err)) | |
return False | |
else: | |
try: | |
client.upload_file(artefact, bucket, bucket_key) | |
except ClientError as err: | |
print("Failed to upload artefact to S3.\n" + str(err)) | |
return False | |
except IOError as err: | |
print("Failed to access artefact in this directory.\n" + str(err)) | |
return False | |
return True | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("bucket", help="Name of the existing S3 bucket") | |
parser.add_argument("artefact", help="Name of the artefact to be uploaded to S3") | |
parser.add_argument("is_folder", help="True if its the name of a folder") | |
parser.add_argument("bucket_key", help="Name of the S3 Bucket key. Usually the file name.") | |
args = parser.parse_args() | |
if not upload_to_s3(args.bucket, args.artefact, args.is_folder, args.bucket_key): | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
@derekedelaney will this script upload to only artefact bucket? I get the following error
python s3_upload.py bucket artefact bucket_key
<1s
+ python s3_upload.py bucket artefact bucket_key
Failed to access artefact in this directory.
[Errno 2] No such file or directory: 'artefact'
Bucket on my s3 is called www.website.com, I changed artefact to www.website.com and I get the following error
python s3_upload.py bucket www.website.com bucket_key
<1s
+ python s3_upload.py bucket www.website.com bucket_key
File "s3_upload.py", line 23
def upload_to_s3(bucket, www.website.com, bucket_key):
^
SyntaxError: invalid syntax
Can you help?
@shovandigital It only works with artifact bucket. I haven't tried with a url before. You might try python s3_upload.py bucket "www.website.com" bucket_key
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run using
python s3_upload.py bucket_name src true _optional_