mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-23 15:57:23 -04:00
Provide s3 credentials for each individual bucket
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import uuid
|
||||
|
||||
from boto3 import resource
|
||||
from boto3 import Session
|
||||
from flask import current_app
|
||||
from notifications_utils.s3 import s3upload as utils_s3upload
|
||||
|
||||
from app.s3_client import get_s3_object
|
||||
|
||||
TEMP_TAG = 'temp-{user_id}_'
|
||||
EMAIL_LOGO_LOCATION_STRUCTURE = '{temp}{unique_id}-{filename}'
|
||||
LETTER_PREFIX = 'letters/static/images/letter-template/'
|
||||
@@ -11,30 +13,33 @@ LETTER_TEMP_TAG = LETTER_PREFIX + TEMP_TAG
|
||||
LETTER_TEMP_LOGO_LOCATION = 'letters/static/images/letter-template/temp-{user_id}_{unique_id}-{filename}'
|
||||
|
||||
|
||||
def get_s3_object(bucket_name, filename):
|
||||
# To inspect contents: obj.get()['Body'].read().decode('utf-8')
|
||||
s3 = resource('s3')
|
||||
obj = s3.Object(bucket_name, filename)
|
||||
return obj
|
||||
def get_logo_location(filename=None):
|
||||
return (
|
||||
current_app.config['LOGO_UPLOAD_BUCKET_NAME'],
|
||||
filename,
|
||||
current_app.config['LOGO_UPLOAD_ACCESS_KEY'],
|
||||
current_app.config['LOGO_UPLOAD_SECRET_KEY'],
|
||||
)
|
||||
|
||||
|
||||
def delete_s3_object(filename):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, filename).delete()
|
||||
get_s3_object(*get_logo_location(filename)).delete()
|
||||
|
||||
|
||||
def persist_logo(old_name, new_name):
|
||||
if old_name == new_name:
|
||||
return
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
get_s3_object(bucket_name, new_name).copy_from(
|
||||
bucket_name, filename, access_key, secret_key = get_logo_location(new_name)
|
||||
get_s3_object(bucket_name, filename, access_key, secret_key).copy_from(
|
||||
CopySource='{}/{}'.format(bucket_name, old_name))
|
||||
delete_s3_object(old_name)
|
||||
|
||||
|
||||
def get_s3_objects_filter_by_prefix(prefix):
|
||||
bucket_name = current_app.config['LOGO_UPLOAD_BUCKET_NAME']
|
||||
s3 = resource('s3')
|
||||
session = Session(aws_access_key_id=current_app.config['LOGO_UPLOAD_ACCESS_KEY'],
|
||||
aws_secret_access_key=current_app.config['LOGO_UPLOAD_SECRET_KEY'])
|
||||
s3 = session.resource('s3')
|
||||
return s3.Bucket(bucket_name).objects.filter(Prefix=prefix)
|
||||
|
||||
|
||||
@@ -58,7 +63,9 @@ def upload_email_logo(filename, filedata, region, user_id):
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=upload_file_name,
|
||||
content_type='image/png'
|
||||
content_type='image/png',
|
||||
access_key=current_app.config['LOGO_UPLOAD_ACCESS_KEY'],
|
||||
secret_key=current_app.config['LOGO_UPLOAD_SECRET_KEY'],
|
||||
)
|
||||
|
||||
return upload_file_name
|
||||
@@ -76,7 +83,9 @@ def upload_letter_temp_logo(filename, filedata, region, user_id):
|
||||
region=region,
|
||||
bucket_name=bucket_name,
|
||||
file_location=upload_filename,
|
||||
content_type='image/svg+xml'
|
||||
content_type='image/svg+xml',
|
||||
access_key=current_app.config['LOGO_UPLOAD_ACCESS_KEY'],
|
||||
secret_key=current_app.config['LOGO_UPLOAD_SECRET_KEY'],
|
||||
)
|
||||
|
||||
return upload_filename
|
||||
|
||||
Reference in New Issue
Block a user