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.
This commit is contained in:
Adeodato Simó 2024-01-28 22:00:40 -03:00
parent 2c231acebe
commit c106b2a988
No known key found for this signature in database
GPG key ID: CDF447845F1A986F

View file

@ -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