2016-05-15 07:47:50 +01:00
|
|
|
import uuid
|
2016-02-02 22:26:49 +00:00
|
|
|
import botocore
|
2016-01-13 17:32:40 +00:00
|
|
|
from boto3 import resource
|
2016-04-07 12:56:44 +01:00
|
|
|
from flask import current_app
|
2017-04-06 10:22:20 +01:00
|
|
|
from notifications_utils.s3 import s3upload as utils_s3upload
|
2016-01-13 17:32:40 +00:00
|
|
|
|
2016-04-07 12:56:44 +01:00
|
|
|
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
2016-02-05 16:13:06 +00:00
|
|
|
|
|
|
|
|
|
2016-05-15 07:47:50 +01:00
|
|
|
def s3upload(service_id, filedata, region):
|
|
|
|
|
upload_id = str(uuid.uuid4())
|
2016-04-07 12:56:44 +01:00
|
|
|
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
2017-04-06 10:22:20 +01:00
|
|
|
utils_s3upload(filedata=filedata['data'],
|
|
|
|
|
region=region,
|
|
|
|
|
bucket_name=current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
|
|
|
|
file_location=upload_file_name)
|
2016-05-15 07:47:50 +01:00
|
|
|
return upload_id
|
|
|
|
|
|
2016-01-14 16:00:13 +00:00
|
|
|
|
|
|
|
|
def s3download(service_id, upload_id):
|
2016-02-05 16:13:06 +00:00
|
|
|
contents = ''
|
|
|
|
|
try:
|
|
|
|
|
s3 = resource('s3')
|
2016-04-07 12:56:44 +01:00
|
|
|
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
|
|
|
|
|
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
2016-02-05 16:13:06 +00:00
|
|
|
key = s3.Object(bucket_name, upload_file_name)
|
|
|
|
|
contents = key.get()['Body'].read().decode('utf-8')
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
2016-04-07 12:56:44 +01:00
|
|
|
current_app.logger.error("Unable to download s3 file {}".format(
|
|
|
|
|
FILE_LOCATION_STRUCTURE.format(service_id, upload_id)))
|
|
|
|
|
raise e
|
2016-01-14 16:00:13 +00:00
|
|
|
return contents
|