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

View File

@@ -24,8 +24,6 @@ class JobApiClient(BaseAPIClient):
"id": job_id, "id": job_id,
"template": template_id, "template": template_id,
"original_file_name": original_file_name, "original_file_name": original_file_name,
"bucket_name": "service-{}-notify".format(service_id),
"file_name": "{}.csv".format(job_id),
"notification_count": notification_count "notification_count": notification_count
} }

View File

@@ -35,6 +35,8 @@ class Config(object):
SHOW_STYLEGUIDE = True SHOW_STYLEGUIDE = True
TOKEN_MAX_AGE_SECONDS = 3600 TOKEN_MAX_AGE_SECONDS = 3600
WTF_CSRF_ENABLED = True WTF_CSRF_ENABLED = True
CSV_UPLOAD_BUCKET_NAME = '{}-notifications-csv-upload'.format(
os.environ['NOTIFY_ADMIN_ENVIRONMENT'].split('config.')[1].lower())
EMAIL_DOMAIN_REGEXES = [ EMAIL_DOMAIN_REGEXES = [
"gov\.uk", "gov\.uk",

View File

@@ -91,8 +91,6 @@ def job_json():
'service': 1, 'service': 1,
'template': 1, 'template': 1,
'original_file_name': 'thisisatest.csv', 'original_file_name': 'thisisatest.csv',
'bucket_name': 'service-1-{}-notify'.format(job_id),
'file_name': '{}.csv'.format(job_id),
'created_at': created_at, 'created_at': created_at,
'notification_count': 1, 'notification_count': 1,
'notifications_sent': 1, 'notifications_sent': 1,

View File

@@ -13,8 +13,6 @@ def test_client_creates_job_data_correctly(mocker):
"id": job_id, "id": job_id,
"template": template_id, "template": template_id,
"original_file_name": original_file_name, "original_file_name": original_file_name,
"bucket_name": "service-{}-notify".format(service_id),
"file_name": "{}.csv".format(job_id),
"notification_count": 1 "notification_count": 1
} }