change it to a task

This commit is contained in:
Kenneth Kehl
2024-06-14 09:32:58 -07:00
parent 9408c9955b
commit bff2df514f
8 changed files with 104 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
import re
import uuid
import botocore
from boto3 import Session
@@ -7,6 +8,7 @@ from flask import current_app
from app import redis_store
from app.clients import AWS_CLIENT_CONFIG
from notifications_utils.s3 import s3upload as utils_s3upload
FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
@@ -19,11 +21,31 @@ JOBS_CACHE_HITS = "JOBS_CACHE_HITS"
JOBS_CACHE_MISSES = "JOBS_CACHE_MISSES"
def get_csv_location(service_id, upload_id):
return (
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
FILE_LOCATION_STRUCTURE.format(service_id, upload_id),
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"],
)
def get_s3_file(bucket_name, file_location, access_key, secret_key, region):
s3_file = get_s3_object(bucket_name, file_location, access_key, secret_key, region)
return s3_file.get()["Body"].read().decode("utf-8")
def get_file_from_s3(file_location):
return get_s3_file(
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
file_location,
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"],
)
def get_s3_object(bucket_name, file_location, access_key, secret_key, region):
session = Session(
aws_access_key_id=access_key,
@@ -253,3 +275,21 @@ def remove_csv_object(object_key):
current_app.config["CSV_UPLOAD_BUCKET"]["region"],
)
return obj.delete()
def s3upload(service_id, filedata, upload_id=None):
if upload_id is None:
upload_id = str(uuid.uuid4())
bucket_name, file_location, access_key, secret_key, region = get_csv_location(
service_id, upload_id
)
utils_s3upload(
filedata=filedata["data"],
region=region,
bucket_name=bucket_name,
file_location=file_location,
access_key=access_key,
secret_key=secret_key,
)
return upload_id