Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.

Commit 32e3667

Browse files
committed
Add region name to session (#6)
1 parent 6087263 commit 32e3667

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

s3.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class S3Uri:
168168
def __init__(self, request, raw_uri):
169169
self.request = request
170170
self.uri = urlparse.urlparse(raw_uri)
171+
match = re.match('(.+\.|)?s3(?:-([^.]*))?.amazonaws.com', self.uri.hostname)
172+
self.virtual_host_bucket, self.region = (match.groups() if match else (None, None))
171173

172174
def user_host(self):
173175
parts = self.uri.netloc.split('@', 1)
@@ -186,13 +188,8 @@ def credentials(self):
186188
raise Exception('Access key and secret are specified improperly in the URL')
187189
return None, None
188190

189-
def virtual_host_bucket(self):
190-
virtual_host_match = re.match('(?:(.*).|)s3(?:-[^.]*)?.amazonaws.com', self.uri.hostname)
191-
return virtual_host_match and virtual_host_match.group(1)
192-
193191
def bucket_key(self):
194-
bucket = self.virtual_host_bucket()
195-
if bucket:
192+
if self.virtual_host_bucket:
196193
key = self.uri.path[1:]
197194
else:
198195
_, bucket, key = map(urllib.unquote, self.uri.path.split('/', 2))
@@ -201,7 +198,7 @@ def bucket_key(self):
201198
def signature_version(self):
202199
if self.request.signature_version:
203200
return self.request.signature_version
204-
elif self.virtual_host_bucket() == '':
201+
elif self.virtual_host_bucket == '':
205202
return 's3v4'
206203

207204
def _handle_message(self, message):
@@ -222,6 +219,7 @@ def _handle_message(self, message):
222219
session = boto3.session.Session(
223220
aws_access_key_id=s3_access_key,
224221
aws_secret_access_key=s3_access_secret,
222+
region_name=s3_uri.region,
225223
)
226224
s3 = session.resource('s3',
227225
config=botocore.client.Config(signature_version=s3_uri.signature_version()),

0 commit comments

Comments
 (0)