Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirFileSystem.ls(path, detail=True) hits AssertionError with S3FileSystem #1638

Open
metadaddy opened this issue Jun 27, 2024 · 0 comments
Open

Comments

@metadaddy
Copy link
Contributor

metadaddy commented Jun 27, 2024

This is a similar issue to #924.

Code

fs = DirFileSystem(f'/{bucket_name}', S3FileSystem())

files = fs.ls('/')

Error

Traceback (most recent call last):
  File "/Users/ppatterson/src/b2_zip_files/app.py", line 40, in <module>
    files = fs.ls('/')
            ^^^^^^^^^^
  File "/Users/ppatterson/live_read_demo/b2_zip_files/lib/python3.11/site-packages/fsspec/implementations/dirfs.py", line 253, in ls
    entry["name"] = self._relpath(entry["name"])
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ppatterson/live_read_demo/b2_zip_files/lib/python3.11/site-packages/fsspec/implementations/dirfs.py", line 70, in _relpath
    assert path.startswith(prefix)
AssertionError

The problem is that DirFileSystem.__init__ sets self.path via fs._strip_protocol(path), which returns /bucket_name. However, S3FileSystem._ls returns filenames of the form bucket_name/path/to/file.ext (no leading /). Hence, in the following code from DirFileSystem._relpath, the assertion fails:

prefix = self.path + self.fs.sep # '/bucket_name/'
assert path.startswith(prefix) # 'bucket_name/path/to/file.ext' does not start with '/bucket-name/'!

It doesn't look like it's possible to set a flag to have S3FileSystem prepend a / to paths, so I think the solution is to make the leading / optional in DirFileSystem._relpath. Something like:

    def _relpath(self, path):
        if isinstance(path, str):
            if not self.path:
                return path
            # We need to account for S3FileSystem returning paths that do not
            # start with a '/'
            if path == self.path or (self.path.startswith(self.fs.sep) and path == self.path[1:]):
                return ""
            prefix = self.path + self.fs.sep
            if self.path.startswith(self.fs.sep) and not path.startswith(self.fs.sep):
                prefix = prefix[1:]
            assert path.startswith(prefix)
            return path[len(prefix) :]
        return [self._relpath(_path) for _path in path]

I'll file a PR with this change.

metadaddy added a commit to metadaddy/filesystem_spec that referenced this issue Jun 27, 2024
metadaddy added a commit to metadaddy/filesystem_spec that referenced this issue Jun 27, 2024
metadaddy added a commit to metadaddy/filesystem_spec that referenced this issue Jun 27, 2024
metadaddy added a commit to metadaddy/filesystem_spec that referenced this issue Jun 27, 2024
martindurant added a commit that referenced this issue Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant