From 12f54264c46af2bca36382feec634c3430119c9e Mon Sep 17 00:00:00 2001 From: pbrw Date: Tue, 25 Jul 2023 01:13:49 +0200 Subject: [PATCH] Add custom path support to URL building --- minio/helpers.py | 13 ++++++++++--- tests/unit/minio_test.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/minio/helpers.py b/minio/helpers.py index b955d8790..c57515314 100644 --- a/minio/helpers.py +++ b/minio/helpers.py @@ -512,9 +512,9 @@ def virtual_style_flag(self, flag): """Check to use virtual style or not.""" self._virtual_style_flag = flag - def build( - self, method, region, - bucket_name=None, object_name=None, query_params=None, + def build( # pylint: disable=too-many-branches + self, method, region, + bucket_name=None, object_name=None, query_params=None, path=None ): """Build URL for given information.""" @@ -523,6 +523,11 @@ def build( f"empty bucket name for object name {object_name}", ) + if path and bucket_name: + raise ValueError( + "path and bucket name can't be set at the same time" + ) + query = [] for key, values in sorted((query_params or {}).items()): values = values if isinstance(values, (list, tuple)) else [values] @@ -535,6 +540,8 @@ def build( if not bucket_name: url = url_replace(url, path="/") + if path: + url = url_replace(url, path=path) return ( url_replace(url, netloc="s3." + region + "." + host) if self._is_aws_host else url diff --git a/tests/unit/minio_test.py b/tests/unit/minio_test.py index 38d9312a2..7268c0af2 100644 --- a/tests/unit/minio_test.py +++ b/tests/unit/minio_test.py @@ -77,6 +77,14 @@ def test_url_build(self): ), 'http://localhost:9000/bucket-name/path/to/objectName/', ) + self.assertEqual( + urlunsplit( + url.build("GET", 'us-east-1', path='some/method/path'), + ), + 'http://localhost:9000/some/method/path', + ) + self.assertRaises(ValueError, url.build, "GET", 'us-east-1', + bucket_name='bucket-name', path='some/method/path') # S3 urls. url = BaseURL('https://s3.amazonaws.com', None) @@ -106,6 +114,12 @@ def test_url_build(self): "https://bucket-name.s3.us-east-1.amazonaws.com" "/objectName?versionId=uuid", ) + self.assertEqual( + urlunsplit( + url.build("GET", 'us-east-1', path='some/method/path'), + ), + 'https://s3.us-east-1.amazonaws.com/some/method/path', + ) def test_minio_requires_string(self): self.assertRaises(TypeError, Minio, 10)