Skip to content

Commit

Permalink
Add custom path support to URL building
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrw committed Jul 25, 2023
1 parent 4ee1892 commit 12f5426
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions minio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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]
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/minio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 12f5426

Please sign in to comment.