diff --git a/storages/backends/s3boto3.py b/storages/backends/s3boto3.py index 338052dbe..3e487d17e 100644 --- a/storages/backends/s3boto3.py +++ b/storages/backends/s3boto3.py @@ -573,6 +573,19 @@ def url(self, name, parameters=None, expire=None, http_method=None): params['Key'] = name url = self.bucket.meta.client.generate_presigned_url('get_object', Params=params, ExpiresIn=expire, HttpMethod=http_method) + + 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)