Skip to content

Commit

Permalink
[s3] Cache Cloudfront Signers (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
scuml authored Jul 6, 2024
1 parent 8271347 commit e268efa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3
- Pull ``AWS_SESSION_TOKEN`` from the environment (`#1399`_)
- Fix newline handling for text mode files (`#1381`_)
- Do not sign URLs when ``querystring_auth=False`` e.g public buckets or static files (`#1402`_)
- Cache CloudFront Signers (`#1417`_)

Azure
-----
Expand All @@ -22,6 +23,7 @@ Azure
.. _#1402: https://github.com/jschneier/django-storages/pull/1402
.. _#1403: https://github.com/jschneier/django-storages/pull/1403
.. _#1414: https://github.com/jschneier/django-storages/pull/1414
.. _#1417: https://github.com/jschneier/django-storages/pull/1417


1.14.3 (2024-05-04)
Expand Down
9 changes: 8 additions & 1 deletion storages/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ class S3Storage(CompressStorageMixin, BaseStorage):
# settings/args are ignored.
config = None

_signers = {} # noqa: RUF012

def __init__(self, **settings):
omitted = object()
if not hasattr(self, "cloudfront_signer"):
Expand Down Expand Up @@ -372,7 +374,12 @@ def __init__(self, **settings):
self.cloudfront_signer = None

def get_cloudfront_signer(self, key_id, key):
return _cloud_front_signer_from_pem(key_id, key)
cache_key = f"{key_id}:{key}"
if cache_key not in self.__class__._signers:
self.__class__._signers[cache_key] = _cloud_front_signer_from_pem(
key_id, key
)
return self.__class__._signers[cache_key]

def get_default_settings(self):
return {
Expand Down

0 comments on commit e268efa

Please sign in to comment.