From 25c7bf506ed87249746dff20b310237c018550a7 Mon Sep 17 00:00:00 2001 From: Brian Helba Date: Sun, 31 Aug 2025 17:59:40 -0400 Subject: [PATCH] Allow `querystring_auth` to be overridden on a per-call basis This preserves the existing behavior by default, but adds the ability for individual calls to `S3Storage.url` to specify a different value of `querystring_auth`. This supports the use case of buckets with mixed public and private content. --- storages/backends/s3.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/storages/backends/s3.py b/storages/backends/s3.py index fa2d39f16..e1d387889 100644 --- a/storages/backends/s3.py +++ b/storages/backends/s3.py @@ -665,12 +665,14 @@ def get_modified_time(self, name): else: return make_naive(entry.last_modified) - def url(self, name, parameters=None, expire=None, http_method=None): + def url(self, name, parameters=None, expire=None, http_method=None, querystring_auth=None): # Preserve the trailing slash after normalizing the path. name = self._normalize_name(clean_name(name)) params = parameters.copy() if parameters else {} if expire is None: expire = self.querystring_expire + if querystring_auth is None: + querystring_auth = self.querystring_auth if self.custom_domain: url = "{}//{}/{}{}".format( @@ -680,7 +682,7 @@ def url(self, name, parameters=None, expire=None, http_method=None): "?{}".format(urlencode(params)) if params else "", ) - if self.querystring_auth and self.cloudfront_signer: + if querystring_auth and self.cloudfront_signer: expiration = datetime.utcnow() + timedelta(seconds=expire) return self.cloudfront_signer.generate_presigned_url( url, date_less_than=expiration @@ -692,7 +694,7 @@ def url(self, name, parameters=None, expire=None, http_method=None): params["Key"] = name connection = ( - self.connection if self.querystring_auth else self.unsigned_connection + self.connection if querystring_auth else self.unsigned_connection ) url = connection.meta.client.generate_presigned_url( "get_object", Params=params, ExpiresIn=expire, HttpMethod=http_method