mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-23 15:57:23 -04:00
Merge pull request #145 from alphagov/bucket-creation
Bucket creation needed correct region to be set.
This commit is contained in:
@@ -1,18 +1,32 @@
|
|||||||
|
import botocore
|
||||||
from boto3 import resource
|
from boto3 import resource
|
||||||
|
|
||||||
|
|
||||||
def s3upload(upload_id, service_id, filedata):
|
def s3upload(upload_id, service_id, filedata, region):
|
||||||
s3 = resource('s3')
|
s3 = resource('s3')
|
||||||
bucket_name = 'service-{}-{}-notify'.format(service_id, upload_id)
|
bucket_name = 'service-{}-notify'.format(service_id)
|
||||||
s3.create_bucket(Bucket=bucket_name)
|
|
||||||
contents = '\n'.join(filedata['data'])
|
contents = '\n'.join(filedata['data'])
|
||||||
|
|
||||||
|
bucket = s3.Bucket(bucket_name)
|
||||||
|
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
|
||||||
|
|
||||||
|
if not exists:
|
||||||
|
s3.create_bucket(Bucket=bucket_name,
|
||||||
|
CreateBucketConfiguration={'LocationConstraint': region})
|
||||||
|
|
||||||
key = s3.Object(bucket_name, upload_id)
|
key = s3.Object(bucket_name, upload_id)
|
||||||
key.put(Body=contents, ServerSideEncryption='AES256')
|
key.put(Body=contents, ServerSideEncryption='AES256')
|
||||||
|
|
||||||
|
|
||||||
def s3download(service_id, upload_id):
|
def s3download(service_id, upload_id):
|
||||||
s3 = resource('s3')
|
s3 = resource('s3')
|
||||||
bucket_name = 'service-{}-{}-notify'.format(service_id, upload_id)
|
bucket_name = 'service-{}-notify'.format(service_id)
|
||||||
key = s3.Object(bucket_name, upload_id)
|
key = s3.Object(bucket_name, upload_id)
|
||||||
contents = key.get()['Body'].read().decode('utf-8')
|
contents = key.get()['Body'].read().decode('utf-8')
|
||||||
return contents
|
return contents
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ from flask import (
|
|||||||
url_for,
|
url_for,
|
||||||
flash,
|
flash,
|
||||||
abort,
|
abort,
|
||||||
session
|
session,
|
||||||
|
current_app
|
||||||
)
|
)
|
||||||
|
|
||||||
from flask_login import login_required
|
from flask_login import login_required
|
||||||
@@ -41,7 +42,7 @@ def send_sms(service_id):
|
|||||||
filedata = _get_filedata(csv_file)
|
filedata = _get_filedata(csv_file)
|
||||||
upload_id = str(uuid.uuid4())
|
upload_id = str(uuid.uuid4())
|
||||||
template_id = request.form.get('template')
|
template_id = request.form.get('template')
|
||||||
s3upload(upload_id, service_id, filedata)
|
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
||||||
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
||||||
return redirect(url_for('.check_sms',
|
return redirect(url_for('.check_sms',
|
||||||
service_id=service_id,
|
service_id=service_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user