2024-05-16 10:17:45 -04:00
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
|
import botocore
|
|
|
|
|
from boto3 import Session
|
|
|
|
|
from botocore.config import Config
|
|
|
|
|
from flask import current_app
|
|
|
|
|
|
2025-08-06 10:59:32 -07:00
|
|
|
from app.config import _s3_credentials_from_env
|
|
|
|
|
|
2024-05-16 10:17:45 -04:00
|
|
|
AWS_CLIENT_CONFIG = Config(
|
|
|
|
|
# This config is required to enable S3 to connect to FIPS-enabled
|
|
|
|
|
# endpoints. See https://aws.amazon.com/compliance/fips/ for more
|
|
|
|
|
# information.
|
|
|
|
|
s3={
|
|
|
|
|
"addressing_style": "virtual",
|
|
|
|
|
},
|
2025-01-09 11:28:24 -08:00
|
|
|
max_pool_connections=50,
|
2024-05-16 10:17:45 -04:00
|
|
|
use_fips_endpoint=True,
|
|
|
|
|
)
|
2025-08-06 10:59:32 -07:00
|
|
|
default_regions = "us-gov-west-1"
|
2024-05-16 10:17:45 -04:00
|
|
|
|
|
|
|
|
|
2025-01-09 07:47:47 -08:00
|
|
|
def get_s3_resource():
|
2025-08-06 10:59:32 -07:00
|
|
|
|
|
|
|
|
credentials = _s3_credentials_from_env("CSV")
|
2025-08-06 10:17:56 -07:00
|
|
|
session = Session(
|
2025-08-06 10:59:32 -07:00
|
|
|
aws_access_key_id=credentials["access_key_id"],
|
|
|
|
|
aws_secret_access_key=credentials["secret_access_key"],
|
|
|
|
|
region_name=credentials["region"],
|
2025-08-06 10:17:56 -07:00
|
|
|
)
|
|
|
|
|
noti_s3_resource = session.resource("s3", config=AWS_CLIENT_CONFIG)
|
2025-01-09 11:28:24 -08:00
|
|
|
return noti_s3_resource
|
2025-01-09 07:47:47 -08:00
|
|
|
|
|
|
|
|
|
2024-05-16 10:17:45 -04:00
|
|
|
def s3upload(
|
|
|
|
|
filedata,
|
|
|
|
|
region,
|
|
|
|
|
bucket_name,
|
|
|
|
|
file_location,
|
|
|
|
|
content_type="binary/octet-stream",
|
|
|
|
|
tags=None,
|
|
|
|
|
metadata=None,
|
|
|
|
|
):
|
2025-01-09 07:47:47 -08:00
|
|
|
_s3 = get_s3_resource()
|
2024-05-16 10:17:45 -04:00
|
|
|
|
|
|
|
|
key = _s3.Object(bucket_name, file_location)
|
|
|
|
|
|
|
|
|
|
put_args = {
|
|
|
|
|
"Body": filedata,
|
|
|
|
|
"ServerSideEncryption": "AES256",
|
|
|
|
|
"ContentType": content_type,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tags:
|
|
|
|
|
tags = urllib.parse.urlencode(tags)
|
|
|
|
|
put_args["Tagging"] = tags
|
|
|
|
|
|
|
|
|
|
if metadata:
|
|
|
|
|
metadata = put_args["Metadata"] = metadata
|
|
|
|
|
|
|
|
|
|
try:
|
2025-08-06 09:23:25 -07:00
|
|
|
current_app.logger.info(f"Going to try to upload this {key}")
|
2024-05-16 10:17:45 -04:00
|
|
|
key.put(**put_args)
|
2025-08-06 09:23:25 -07:00
|
|
|
except botocore.exceptions.NoCredentialsError as e:
|
|
|
|
|
current_app.logger.exception(
|
|
|
|
|
f"Unable to upload {key} to S3 bucket because of {e}"
|
|
|
|
|
)
|
2025-08-06 10:17:56 -07:00
|
|
|
raise e
|
2024-05-16 10:17:45 -04:00
|
|
|
except botocore.exceptions.ClientError as e:
|
2025-08-06 09:23:25 -07:00
|
|
|
current_app.logger.exception(
|
|
|
|
|
f"Unable to upload {key}to S3 bucket because of {e}"
|
|
|
|
|
)
|
2024-05-16 10:17:45 -04:00
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class S3ObjectNotFound(botocore.exceptions.ClientError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def s3download(
|
|
|
|
|
bucket_name,
|
|
|
|
|
filename,
|
|
|
|
|
):
|
|
|
|
|
try:
|
2025-01-09 07:47:47 -08:00
|
|
|
s3 = get_s3_resource()
|
2024-05-16 10:17:45 -04:00
|
|
|
key = s3.Object(bucket_name, filename)
|
|
|
|
|
return key.get()["Body"]
|
|
|
|
|
except botocore.exceptions.ClientError as error:
|
|
|
|
|
raise S3ObjectNotFound(error.response, error.operation_name)
|