Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix presigned url endpoint #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions s3upload/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
5 changes: 3 additions & 2 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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)

Expand Down