From efb74ce31c3051aa9a4a7aa1669f46ed480e0a4e Mon Sep 17 00:00:00 2001 From: rileypeterson Date: Mon, 24 Sep 2018 01:31:12 -0700 Subject: [PATCH] Fix url parsing for `S3` (#235) --- smart_open/smart_open_lib.py | 4 ++-- smart_open/tests/test_smart_open.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/smart_open/smart_open_lib.py b/smart_open/smart_open_lib.py index bb337e65..a6b82f6b 100644 --- a/smart_open/smart_open_lib.py +++ b/smart_open/smart_open_lib.py @@ -84,7 +84,7 @@ Uri = collections.namedtuple( - 'Uri', + 'Uri', ( 'scheme', 'uri_path', @@ -488,7 +488,7 @@ def _parse_uri_s3x(parsed_uri): try: uri = parsed_uri.netloc + parsed_uri.path # Separate authentication from URI if exist - if ':' in uri.split('@')[0]: + if ':' in uri.split('@')[0] and '@' in uri: auth, uri = uri.split('@', 1) access_id, access_secret = auth.split(':') else: diff --git a/smart_open/tests/test_smart_open.py b/smart_open/tests/test_smart_open.py index 99f83ee3..ec2dccb5 100644 --- a/smart_open/tests/test_smart_open.py +++ b/smart_open/tests/test_smart_open.py @@ -128,6 +128,15 @@ def test_uri_from_issue_223_works(self): self.assertEqual(parsed_uri.access_id, "") self.assertEqual(parsed_uri.access_secret, "") + def test_s3_uri_with_colon_in_key_name(self): + """ Correctly parse the s3 url if there is a colon in the key or dir """ + parsed_uri = smart_open_lib._parse_uri("s3://mybucket/mydir/my:key") + self.assertEqual(parsed_uri.scheme, "s3") + self.assertEqual(parsed_uri.bucket_id, "mybucket") + self.assertEqual(parsed_uri.key_id, "mydir/my:key") + self.assertEqual(parsed_uri.access_id, None) + self.assertEqual(parsed_uri.access_secret, None) + class SmartOpenHttpTest(unittest.TestCase): """