2019-01-30 09:42:15 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
from app.s3_client import (
|
|
|
|
|
get_s3_contents,
|
|
|
|
|
get_s3_metadata,
|
|
|
|
|
get_s3_object,
|
|
|
|
|
set_s3_metadata,
|
|
|
|
|
)
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.s3 import s3upload as utils_s3upload
|
2019-01-30 09:42:15 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
|
2019-01-30 09:42:15 +00:00
|
|
|
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
def get_csv_location(service_id, upload_id):
|
2019-01-30 09:42:15 +00:00
|
|
|
return (
|
2023-08-25 09:12:23 -07:00
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
|
2019-01-30 09:42:15 +00:00
|
|
|
FILE_LOCATION_STRUCTURE.format(service_id, upload_id),
|
2023-08-25 09:12:23 -07:00
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["access_key_id"],
|
|
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["secret_access_key"],
|
|
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["region"],
|
2019-01-30 09:42:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
def get_csv_upload(service_id, upload_id):
|
|
|
|
|
return get_s3_object(*get_csv_location(service_id, upload_id))
|
2019-01-30 09:42:15 +00:00
|
|
|
|
|
|
|
|
|
2022-09-26 10:25:03 -04:00
|
|
|
def s3upload(service_id, filedata):
|
2024-05-29 14:18:08 -07:00
|
|
|
|
2019-01-30 09:42:15 +00:00
|
|
|
upload_id = str(uuid.uuid4())
|
2023-08-25 09:12:23 -07:00
|
|
|
bucket_name, file_location, access_key, secret_key, region = get_csv_location(
|
|
|
|
|
service_id, upload_id
|
|
|
|
|
)
|
2019-01-30 09:42:15 +00:00
|
|
|
utils_s3upload(
|
2023-08-25 09:12:23 -07:00
|
|
|
filedata=filedata["data"],
|
2019-01-30 09:42:15 +00:00
|
|
|
region=region,
|
|
|
|
|
bucket_name=bucket_name,
|
|
|
|
|
file_location=file_location,
|
2022-09-22 12:33:55 -04:00
|
|
|
access_key=access_key,
|
|
|
|
|
secret_key=secret_key,
|
2019-01-30 09:42:15 +00:00
|
|
|
)
|
|
|
|
|
return upload_id
|
|
|
|
|
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
def s3download(service_id, upload_id):
|
|
|
|
|
return get_s3_contents(get_csv_upload(service_id, upload_id))
|
2019-01-30 09:42:15 +00:00
|
|
|
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
def set_metadata_on_csv_upload(service_id, upload_id, **kwargs):
|
|
|
|
|
return set_s3_metadata(get_csv_upload(service_id, upload_id), **kwargs)
|
2020-03-12 16:13:18 +00:00
|
|
|
|
|
|
|
|
|
2022-09-22 12:33:55 -04:00
|
|
|
def get_csv_metadata(service_id, upload_id):
|
|
|
|
|
return get_s3_metadata(get_csv_upload(service_id, upload_id))
|