Skip to content

Instantly share code, notes, and snippets.

@evanfrisch
Created October 31, 2017 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanfrisch/40ad4cc464a5384ca8437f87b7e16945 to your computer and use it in GitHub Desktop.
Save evanfrisch/40ad4cc464a5384ca8437f87b7e16945 to your computer and use it in GitHub Desktop.
import json
import boto3
# Create S3 bucket if it does not exist already
s3 = boto3.resource('s3')
bucket_name = 'so_predict'
s3.create_bucket(Bucket=bucket_name)
# Dictionary mapping local filenames to the filenames to use in S3 bucket
fileDict = { 'test_with_combined_qf_predictions.json': 'questions_qf.json', 'quantile_metrics_questions.json': 'questions_quantiles.json',
'test_with_predictions_rq_gbm.json': 'users_rq.json', 'quantile_metrics_users.json': 'users_quantiles.json' }
def upload_file_to_bucket(json_path = 'json', filename_local, filename_s3, bucket_name):
"""Stores the specified file to an S3 bucket using the name provided
:param json_path: the relative path to the local file (Default value = 'json')
:param filename_local: the name of the local file to upload to S3
:param filename_s3: the filename to write to S3
:param bucket_name: the name of the S3 bucket in which to store the file
"""
# Read in the contents of the json file from the local filesystem
data = []
with open(json_path+'\'+filename_local) as json_data:
data = json.load(json_data)
# Write the contents to a json file in the S3 bucket
s3 = boto3.resource('s3')
obj = s3.Object(bucket_name,filename_s3)
obj.put(Body=json.dumps(data))
# Upload each file to the S3 bucket
for filename_local, filename_s3 in fileDict.items():
upload_file_to_bucket(filename_local=filename_local, filename_s3=filename_s3, bucket_name=bucket_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment