From 8beec5a44b471bb650daa4ee27cb6978de72ab7d Mon Sep 17 00:00:00 2001 From: Christian Poetter Date: Fri, 14 Feb 2020 14:19:40 +0100 Subject: [PATCH 1/2] Allow using AWS_QUERYSTRING_AUTH & AWS_S3_CUSTOM_DOMAIN at the same time --- storages/backends/s3boto3.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/storages/backends/s3boto3.py b/storages/backends/s3boto3.py index 55a55419b..0c715a0f1 100644 --- a/storages/backends/s3boto3.py +++ b/storages/backends/s3boto3.py @@ -671,9 +671,7 @@ def _strip_signing_parameters(self, url): def url(self, name, parameters=None, expire=None): # Preserve the trailing slash after normalizing the path. name = self._normalize_name(self._clean_name(name)) - if self.custom_domain: - return "{}//{}/{}".format(self.url_protocol, - self.custom_domain, filepath_to_uri(name)) + if expire is None: expire = self.querystring_expire @@ -682,6 +680,19 @@ def url(self, name, parameters=None, expire=None): params['Key'] = self._encode_name(name) url = self.bucket.meta.client.generate_presigned_url('get_object', Params=params, ExpiresIn=expire) + + if self.custom_domain: + # Key parameter can't be empty. Use "/" and remove it later. + params['Key'] = '/' + root_url_signed = self.bucket.meta.client.generate_presigned_url('get_object', + Params=params, + ExpiresIn=expire) + # Remove signing parameter and previouly added key "/". + root_url = self._strip_signing_parameters(root_url_signed)[:-1] + # Replace bucket domain with custom domain. + custom_url = "{}//{}/".format(self.url_protocol, self.custom_domain) + url = url.replace(root_url, custom_url) + if self.querystring_auth: return url return self._strip_signing_parameters(url) From 10dfd8e5c35e0b046e6bb9ef46d3396ca3e3fdf9 Mon Sep 17 00:00:00 2001 From: Christian Poetter Date: Mon, 6 Jul 2020 15:07:15 +0200 Subject: [PATCH 2/2] Try to fix merge conflicts for #839 --- storages/backends/s3boto3.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/storages/backends/s3boto3.py b/storages/backends/s3boto3.py index 0c715a0f1..fa519ca93 100644 --- a/storages/backends/s3boto3.py +++ b/storages/backends/s3boto3.py @@ -671,7 +671,6 @@ def _strip_signing_parameters(self, url): def url(self, name, parameters=None, expire=None): # Preserve the trailing slash after normalizing the path. name = self._normalize_name(self._clean_name(name)) - if expire is None: expire = self.querystring_expire @@ -679,7 +678,7 @@ def url(self, name, parameters=None, expire=None): params['Bucket'] = self.bucket.name params['Key'] = self._encode_name(name) url = self.bucket.meta.client.generate_presigned_url('get_object', Params=params, - ExpiresIn=expire) + ExpiresIn=expire, HttpMethod=http_method) if self.custom_domain: # Key parameter can't be empty. Use "/" and remove it later.