Upload csv location updated, all tests passing.

This commit is contained in:
Nicholas Staples
2016-04-07 12:56:44 +01:00
parent 194c56943c
commit 8f8cdabfff
5 changed files with 17 additions and 18 deletions

View File

@@ -1,28 +1,33 @@
import botocore
from boto3 import resource
from flask import current_app
BUCKET_NAME = 'service-{}-notify'
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
def s3upload(upload_id, service_id, filedata, region):
s3 = resource('s3')
bucket_name = BUCKET_NAME.format(service_id)
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
contents = filedata['data']
exists = True
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
s3.meta.client.head_bucket(
Bucket=bucket_name)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
else:
current_app.logger.error(
"Unable to create s3 bucket {}".format(bucket_name))
raise e
if not exists:
s3.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region})
upload_file_name = "{}.csv".format(upload_id)
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
key = s3.Object(bucket_name, upload_file_name)
key.put(Body=contents, ServerSideEncryption='AES256')
@@ -31,14 +36,12 @@ def s3download(service_id, upload_id):
contents = ''
try:
s3 = resource('s3')
bucket_name = BUCKET_NAME.format(service_id)
upload_file_name = "{}.csv".format(upload_id)
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
key = s3.Object(bucket_name, upload_file_name)
contents = key.get()['Body'].read().decode('utf-8')
except botocore.exceptions.ClientError as e:
err = e.response['Error']
if err['Code'] == 'NoSuchBucket':
err_msg = '{}:{}'.format(err['BucketName'], err['Message'])
# TODO properly log error
print(err_msg)
current_app.logger.error("Unable to download s3 file {}".format(
FILE_LOCATION_STRUCTURE.format(service_id, upload_id)))
raise e
return contents