2018-07-12 16:53:10 +01:00
|
|
|
import botocore
|
2023-03-03 16:01:12 -05:00
|
|
|
from boto3 import Session
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import current_app
|
2017-06-12 15:55:05 +01:00
|
|
|
|
2023-08-10 18:02:45 -04:00
|
|
|
from app.clients import AWS_CLIENT_CONFIG
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
FILE_LOCATION_STRUCTURE = "service-{}-notify/{}.csv"
|
2016-02-24 17:12:30 +00:00
|
|
|
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def get_s3_file(bucket_name, file_location, access_key, secret_key, region):
|
2022-09-26 10:56:59 -04:00
|
|
|
s3_file = get_s3_object(bucket_name, file_location, access_key, secret_key, region)
|
2023-08-29 14:54:30 -07:00
|
|
|
return s3_file.get()["Body"].read().decode("utf-8")
|
2017-05-12 17:39:15 +01:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def get_s3_object(bucket_name, file_location, access_key, secret_key, region):
|
2023-08-10 18:02:45 -04:00
|
|
|
session = Session(
|
|
|
|
|
aws_access_key_id=access_key,
|
|
|
|
|
aws_secret_access_key=secret_key,
|
2023-08-29 14:54:30 -07:00
|
|
|
region_name=region,
|
2023-08-10 18:02:45 -04:00
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
|
2017-05-12 17:21:07 +01:00
|
|
|
return s3.Object(bucket_name, file_location)
|
2016-04-05 14:28:19 +01:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def file_exists(bucket_name, file_location, access_key, secret_key, region):
|
2018-07-12 16:53:10 +01:00
|
|
|
try:
|
|
|
|
|
# try and access metadata of object
|
2023-08-29 14:54:30 -07:00
|
|
|
get_s3_object(
|
|
|
|
|
bucket_name, file_location, access_key, secret_key, region
|
|
|
|
|
).metadata
|
2018-07-12 16:53:10 +01:00
|
|
|
return True
|
|
|
|
|
except botocore.exceptions.ClientError as e:
|
2023-08-29 14:54:30 -07:00
|
|
|
if e.response["ResponseMetadata"]["HTTPStatusCode"] == 404:
|
2018-07-12 16:53:10 +01:00
|
|
|
return False
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2018-04-30 11:47:13 +01:00
|
|
|
def get_job_location(service_id, job_id):
|
|
|
|
|
return (
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
|
2018-04-30 11:47:13 +01:00
|
|
|
FILE_LOCATION_STRUCTURE.format(service_id, job_id),
|
2023-08-29 14:54:30 -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"],
|
2018-04-30 11:47:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-11-08 10:30:26 +00:00
|
|
|
def get_job_and_metadata_from_s3(service_id, job_id):
|
|
|
|
|
obj = get_s3_object(*get_job_location(service_id, job_id))
|
2023-08-29 14:54:30 -07:00
|
|
|
return obj.get()["Body"].read().decode("utf-8"), obj.get()["Metadata"]
|
2019-11-08 10:30:26 +00:00
|
|
|
|
|
|
|
|
|
2016-04-07 13:44:04 +01:00
|
|
|
def get_job_from_s3(service_id, job_id):
|
2018-04-30 11:47:13 +01:00
|
|
|
obj = get_s3_object(*get_job_location(service_id, job_id))
|
2023-08-29 14:54:30 -07:00
|
|
|
return obj.get()["Body"].read().decode("utf-8")
|
2016-04-05 14:28:19 +01:00
|
|
|
|
|
|
|
|
|
2018-04-30 11:47:13 +01:00
|
|
|
def get_job_metadata_from_s3(service_id, job_id):
|
|
|
|
|
obj = get_s3_object(*get_job_location(service_id, job_id))
|
2023-08-29 14:54:30 -07:00
|
|
|
return obj.get()["Metadata"]
|
2018-04-30 11:47:13 +01:00
|
|
|
|
|
|
|
|
|
2016-04-07 13:44:04 +01:00
|
|
|
def remove_job_from_s3(service_id, job_id):
|
2018-04-30 11:47:13 +01:00
|
|
|
return remove_s3_object(*get_job_location(service_id, job_id))
|
2017-06-12 15:55:05 +01:00
|
|
|
|
|
|
|
|
|
2022-09-26 10:56:59 -04:00
|
|
|
def remove_s3_object(bucket_name, object_key, access_key, secret_key, region):
|
|
|
|
|
obj = get_s3_object(bucket_name, object_key, access_key, secret_key, region)
|
2017-05-12 17:21:07 +01:00
|
|
|
return obj.delete()
|
2023-05-23 08:31:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_csv_object(object_key):
|
|
|
|
|
obj = get_s3_object(
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app.config["CSV_UPLOAD_BUCKET"]["bucket"],
|
2023-05-23 08:31:30 -07:00
|
|
|
object_key,
|
2023-08-29 14:54:30 -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"],
|
2023-05-23 08:31:30 -07:00
|
|
|
)
|
|
|
|
|
return obj.delete()
|