From 98d690620a81d217b77f855944efa7d3d4b17c08 Mon Sep 17 00:00:00 2001 From: John Tordoff <> Date: Tue, 8 Aug 2023 16:14:22 -0400 Subject: [PATCH] ensure relative path works properly for s3 improvements --- tests/providers/s3/test_provider.py | 35 ++++++++++++++++++++++++---- waterbutler/providers/s3/metadata.py | 2 ++ waterbutler/providers/s3/provider.py | 12 ++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/tests/providers/s3/test_provider.py b/tests/providers/s3/test_provider.py index cdfb36cda..ddee6756d 100644 --- a/tests/providers/s3/test_provider.py +++ b/tests/providers/s3/test_provider.py @@ -210,11 +210,36 @@ class TestValidatePath: async def test_validate_v1_path_file(self, provider, file_header_metadata, mock_time): file_path = 'foobah' - params = {'prefix': '/' + file_path + '/', 'delimiter': '/'} - good_metadata_url = provider.bucket.new_key('/' + file_path).generate_url(100, 'HEAD') - bad_metadata_url = provider.bucket.generate_url(100) - aiohttpretty.register_uri('HEAD', good_metadata_url, headers=file_header_metadata) - aiohttpretty.register_uri('GET', bad_metadata_url, params=params, status=404) + good_metadata_url = provider.bucket.new_key(f'/{file_path}').generate_url(100, 'HEAD') + root_metadata_url = provider.bucket.new_key('/').generate_url(100, 'GET') + aiohttpretty.register_uri( + 'HEAD', + good_metadata_url, + headers=file_header_metadata + ) + aiohttpretty.register_uri( + 'GET', + good_metadata_url, + headers=file_header_metadata + ) + aiohttpretty.register_uri( + 'GET', + root_metadata_url, + params={ + 'prefix': '/' + file_path + '/', + 'delimiter': '/' + }, + status=404 + ) + aiohttpretty.register_uri( + 'GET', + root_metadata_url, + params={ + 'prefix': '/', + 'delimiter': '/' + }, + headers=file_header_metadata + ) assert WaterButlerPath('/') == await provider.validate_v1_path('/') diff --git a/waterbutler/providers/s3/metadata.py b/waterbutler/providers/s3/metadata.py index 3a52c2d8d..7a2343095 100644 --- a/waterbutler/providers/s3/metadata.py +++ b/waterbutler/providers/s3/metadata.py @@ -114,6 +114,8 @@ def name(self): @property def path(self): + if self.raw.get('base_folder'): + return '/' + self.raw['Prefix'].replace(self.raw['base_folder'], '') return '/' + self.raw['Prefix'] diff --git a/waterbutler/providers/s3/provider.py b/waterbutler/providers/s3/provider.py index 93655d2dc..f1678f739 100644 --- a/waterbutler/providers/s3/provider.py +++ b/waterbutler/providers/s3/provider.py @@ -68,8 +68,11 @@ def __init__(self, auth, credentials, settings, **kwargs): async def validate_v1_path(self, path, **kwargs): await self._check_region() - if path == '/': - return WaterButlerPath(path) + base_folder = self.settings.get('id', ':/').split(':/')[1] + path = base_folder + path.lstrip('/') + + if not path.startswith('/'): + path = f'/{path}' implicit_folder = path.endswith('/') @@ -686,6 +689,7 @@ async def _metadata_folder(self, path): await self._check_region() params = {'prefix': path.path, 'delimiter': '/'} + resp = await self.make_request( 'GET', functools.partial(self.bucket.generate_url, settings.TEMP_URL_SECS, 'GET', query_parameters=params), @@ -719,6 +723,10 @@ async def _metadata_folder(self, path): if isinstance(prefixes, dict): prefixes = [prefixes] + # The user selected base folder, the root of the where that user's node is connected. + for item in prefixes: + item['base_folder'] = self.settings.get('id', ':/').split(':/')[1] + items = [ S3FolderMetadata(item) for item in prefixes