From c106b2a988296c4bc4ad65d82a2676b8fd796d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 28 Jan 2024 22:00:40 -0300 Subject: [PATCH] Subclass boto3.Session to use AWS_S3_ENDPOINT_URL As of 0.1.13, the s3-tar library uses an environment variable (`S3_ENDPOINT_URL`) to determine the AWS endpoint. See: https://github.com/xtream1101/s3-tar/blob/0.1.13/s3_tar/utils.py#L25-L29. To save BookWyrm admins from having to set it (e.g., through `.env`) when they are already setting `AWS_S3_ENDPOINT_URL`, we create a Session class that unconditionally uses that URL, and feed it to S3Tar. --- bookwyrm/models/bookwyrm_export_job.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bookwyrm/models/bookwyrm_export_job.py b/bookwyrm/models/bookwyrm_export_job.py index 2d87b203f..610ec13d8 100644 --- a/bookwyrm/models/bookwyrm_export_job.py +++ b/bookwyrm/models/bookwyrm_export_job.py @@ -3,6 +3,7 @@ import logging from uuid import uuid4 +from boto3.session import Session as BotoSession from s3_tar import S3Tar from storages.backends.s3boto3 import S3Boto3Storage @@ -25,6 +26,14 @@ from bookwyrm.utils.tar import BookwyrmTarFile logger = logging.getLogger(__name__) +class BookwyrmAwsSession(BotoSession): + """a boto session that always uses settings.AWS_S3_ENDPOINT_URL""" + + def client(service_name, **kwargs): + kwargs["endpoint_url"] = settings.AWS_S3_ENDPOINT_URL + return super().client(service_name, **kwargs) + + class BookwyrmExportJob(ParentJob): """entry for a specific request to export a bookwyrm user""" @@ -211,6 +220,7 @@ class AddFileToTar(ChildJob): s3_job = S3Tar( settings.AWS_STORAGE_BUCKET_NAME, f"exports/{filename}.tar.gz", + session=BookwyrmAwsSession(), ) # save json file