From dd9757d42717ea4f01f257c836851464ec3b3eb7 Mon Sep 17 00:00:00 2001 From: Chris Pawlukowsky Date: Thu, 15 Mar 2018 19:00:57 -0400 Subject: [PATCH] Resolves #12. Adds support for path-style URLs on top of the already supported virtual-hosted-style URL format. --- awsauth.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/awsauth.py b/awsauth.py index b97e149..689c3f2 100644 --- a/awsauth.py +++ b/awsauth.py @@ -65,13 +65,29 @@ def get_signature(self, r): def get_canonical_string(self, url, headers, method): parsedurl = urlparse(url) - objectkey = parsedurl.path[1:] query_args = sorted(parsedurl.query.split('&')) - - bucket = parsedurl.netloc[:-len(self.service_base_url)] - if len(bucket) > 1: - # remove last dot - bucket = bucket[:-1] + is_virtual_hosted_style_url = parsedurl.netloc.endswith( + '.s3.amazonaws.com') + + if is_virtual_hosted_style_url: + objectkey = parsedurl.path[1:] + bucket = parsedurl.netloc[:-len(self.service_base_url)] + if len(bucket) > 1: + # remove last dot + bucket = bucket[:-1] + else: + # path-style URL + # examples: + # /mybucket + # /mybucket/object + # /mybucket/folder/object + path_split = parsedurl.path[1:].split('/', 1) + bucket = '' + objectkey = '' + if len(path_split) > 0: + bucket = path_split[0] + if len(path_split) > 1: + objectkey = path_split[1] interesting_headers = { 'content-md5': '', @@ -107,8 +123,11 @@ def get_canonical_string(self, url, headers, method): if bucket != '': buf += '/%s' % bucket - # add the objectkey. even if it doesn't exist, add the slash - buf += '/%s' % objectkey + # add the objectkey. + # If this is a virtual-hosted-style URL + # add the slash even if the objectkey doesn't exist + if objectkey or is_virtual_hosted_style_url: + buf += '/%s' % objectkey params_found = False