Skip to content

Commit

Permalink
Merge pull request #171 from octoenergy/fix-bug-with-s3-subdir-list-f…
Browse files Browse the repository at this point in the history
…iles

Fix bug with `list_files`  method in `S3SubdirectoryFileStore`
  • Loading branch information
DHUKK authored Aug 30, 2024
2 parents 9ccdfe2 + e648e6f commit 93eabab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions tests/storage/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ def test_list_files(self, get_boto_bucket):
Prefix="folder/a/b"
)

@mock.patch.object(storage.S3FileStore, "_get_boto_bucket")
def test_list_files_no_path(self, get_boto_bucket):
get_boto_bucket.return_value = mock.Mock(
**{
"objects.filter.return_value": [
mock.Mock(key="folder/a/b/"),
mock.Mock(key="folder/a/b/foo.txt"),
mock.Mock(key="folder/a/b/bar.txt"),
]
}
)
store = storage.S3SubdirectoryFileStore("s3://some-bucket")

assert list(store.list_files(namespace="folder/a/b")) == [
"folder/a/b/foo.txt",
"folder/a/b/bar.txt",
]
# Should be called including the subdirectory path.
get_boto_bucket.return_value.objects.filter.assert_called_once_with(
Prefix="folder/a/b"
)

@mock.patch.object(storage.S3FileStore, "_get_boto_object")
def test_fetch_file_fetches_given_path(self, get_boto_object):
"""Fetch file should act on the given path.
Expand Down
4 changes: 3 additions & 1 deletion xocto/storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,12 @@ def list_s3_keys_page(
return objects, next_token

def list_files(self, namespace: str = "") -> Iterable[str]:
path_trim = 0
if self.path:
path_trim = len(self.path) + 1
namespace = os.path.join(self.path, namespace)
full_paths = super().list_files(namespace)
yield from (path[len(self.path) + 1 :] for path in full_paths)
yield from (path[path_trim:] for path in full_paths)

def copy(self, *, s3_object: S3Object, destination: str) -> S3Object:
if self.path:
Expand Down

0 comments on commit 93eabab

Please sign in to comment.