diff --git a/s3upload/utils.py b/s3upload/utils.py index cc2e580b..3b9cfb29 100644 --- a/s3upload/utils.py +++ b/s3upload/utils.py @@ -142,10 +142,13 @@ def get_signed_download_url( ttl: int = 60, ) -> str: bucket_name = bucket_name or settings.AWS_STORAGE_BUCKET_NAME + bucket_endpoint = get_bucket_endpoint_url(bucket_name) + s3 = boto3.client( "s3", aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, + endpoint_url=bucket_endpoint, ) download_url = s3.generate_presigned_url( "get_object", Params={"Bucket": bucket_name, "Key": key}, ExpiresIn=ttl diff --git a/tests/test_utils.py b/tests/test_utils.py index 13779398..e5d81cc5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,7 @@ +from urllib.parse import urlparse + import pytest +from django.test import override_settings from s3upload.utils import get_bucket_endpoint_url, get_s3_path_from_url @@ -48,3 +51,13 @@ def test_get_bucket_endpoint_url(url: str, expected: str) -> None: get_bucket_endpoint_url(bucket_name=TEST_BUCKET, region=TEST_REGION, **kwargs) == expected ) + + +@override_settings( + S3UPLOAD_REGION=TEST_REGION, +) +def test_get_signed_download_url() -> None: + presigned_url = get_signed_download_url(key=TEST_KEY, bucket_name=TEST_BUCKET) + parsed_url = urlparse(presigned_url) + assert parsed_url.scheme == "https" + assert parsed_url.netloc == f"{TEST_BUCKET}.s3.{TEST_REGION}.amazonaws.com" diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 9c715581..951fce8c 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -181,7 +181,8 @@ def test_check_policy_conditions(self): "acl": "private", "bucket": "test-bucket", } - } + }, + S3UPLOAD_REGION="us", ) def test_check_signed_url(self): data = {"dest": "misc", "name": "image.jpg", "type": "image/jpeg"} @@ -190,7 +191,7 @@ def test_check_signed_url(self): parsed_url = urlparse(response_dict["private_access_url"]) parsed_qs = parse_qs(parsed_url.query) self.assertEqual(parsed_url.scheme, "https") - self.assertEqual(parsed_url.netloc, "test-bucket.s3.amazonaws.com") + self.assertEqual(parsed_url.netloc, "test-bucket.s3.us.amazonaws.com") self.assertTrue("Signature" in parsed_qs) self.assertTrue("Expires" in parsed_qs)