mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 02:23:19 -04:00
Refactor s3upload.
s3upload now calls into the common notifications-utils method to upload a s3 file.
This commit is contained in:
+5
-24
@@ -2,37 +2,18 @@ import uuid
|
|||||||
import botocore
|
import botocore
|
||||||
from boto3 import resource
|
from boto3 import resource
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||||
|
|
||||||
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
FILE_LOCATION_STRUCTURE = 'service-{}-notify/{}.csv'
|
||||||
|
|
||||||
|
|
||||||
def s3upload(service_id, filedata, region):
|
def s3upload(service_id, filedata, region):
|
||||||
s3 = resource('s3')
|
|
||||||
bucket_name = current_app.config['CSV_UPLOAD_BUCKET_NAME']
|
|
||||||
contents = filedata['data']
|
|
||||||
|
|
||||||
exists = True
|
|
||||||
try:
|
|
||||||
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_id = str(uuid.uuid4())
|
upload_id = str(uuid.uuid4())
|
||||||
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
upload_file_name = FILE_LOCATION_STRUCTURE.format(service_id, upload_id)
|
||||||
key = s3.Object(bucket_name, upload_file_name)
|
utils_s3upload(filedata=filedata['data'],
|
||||||
key.put(Body=contents, ServerSideEncryption='AES256')
|
region=region,
|
||||||
|
bucket_name=current_app.config['CSV_UPLOAD_BUCKET_NAME'],
|
||||||
|
file_location=upload_file_name)
|
||||||
return upload_id
|
return upload_id
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user