Last active
July 8, 2018 14:40
-
-
Save keithrozario/eddab0a1336476b2acb16514e5808bf4 to your computer and use it in GitHub Desktop.
Upload to S3 Bucket
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 boto3 | |
| import os | |
| ''' Upload all files + folders in this directory to the <dest_folder> of <bucket_name> | |
| also checks the correct mime types to properly display in browsers | |
| ''' | |
| bucket_name = 'govscan.info' | |
| dest_folder = 'docs' | |
| def upload_to_s3(object_name, s3_dir_name=None): | |
| if s3_dir_name is None: | |
| object_path = dest_folder + "/" + object_name | |
| else: | |
| object_path = s3_dir_name + "/" + object_name | |
| # For content-disposition, otherwise AWS treats all as binary files | |
| if object_name[-4:] == 'html': | |
| extra_args = {'ContentType': 'text/html'} | |
| elif object_name[-3:] == 'jpg' or object_name[-4:] == 'jpeg': | |
| extra_args = {'ContentType': 'image/jpeg'} | |
| elif object_name[-3:] == 'png': | |
| extra_args = {'ContentType': 'image/png'} | |
| elif object_name[-3:] == 'gif': | |
| extra_args = {'ContentType': 'image/gif'} | |
| elif object_name[-3:] == 'css': | |
| extra_args = {'ContentType': 'text/css'} | |
| elif object_name[-2:] == 'js': | |
| extra_args = {'ContentType': 'application/javascript'} | |
| elif object_name[-4:] == 'woff' or object_name[-5:] == 'woff2': | |
| extra_args = {'ContentType': 'font/woff'} | |
| elif object_name[-3:] == 'ttf': | |
| extra_args = {'ContentType': 'font/ttf'} | |
| else: | |
| extra_args = None | |
| # S3 setup | |
| s3 = boto3.resource('s3') # need resource for meta.client.upload_file | |
| s3.meta.client.upload_file(object_name, | |
| bucket_name, | |
| object_path, | |
| ExtraArgs=extra_args | |
| ) | |
| print("Uploaded: %s" % object_path) | |
| print("\t %s" % extra_args) | |
| if __name__ == "__main__": | |
| for directory, subdirectories, files in os.walk('.'): | |
| for file in files: | |
| upload_to_s3(os.path.join(directory, file)[2:]) | |
| client = boto3.client('s3') | |
| response = client.list_objects_v2(Bucket=bucket_name, | |
| Delimiter='|') | |
| for content in response['Contents']: | |
| if content['Key'][:len(dest_folder)] == dest_folder: | |
| file_name = content['Key'][len(dest_folder):] | |
| last_modified = content['LastModified'] | |
| print("%s : %s" % (file_name, last_modified)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment