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)