Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions obstore/python/obstore/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import fsspec.asyn
import fsspec.spec
from fsspec.core import split_protocol
from fsspec.implementations.local import make_path_posix

from obstore import open_reader, open_writer
Expand Down Expand Up @@ -485,9 +486,15 @@ async def _put_file(
await store.put_async(rpath, Path(lpath))

async def _get_file(self, rpath: str, lpath: str, **_kwargs: Any) -> None:
res = urlparse(lpath)
if res.scheme or await self._local_store._isdir(lpath): # noqa: SLF001
# lpath need to be local file and cannot contain scheme
# lpath needs to be a local path. `split_protocol` does not treat a Windows
# drive letter (e.g. `C:`) as a protocol.
protocol, local_path = split_protocol(lpath)
if protocol not in (None, "file"):
err_msg = f"Expected a local path for lpath, got {lpath}"
raise ValueError(err_msg)
lpath = local_path

if await self._local_store._isdir(lpath): # noqa: SLF001
return

resp = await self._cat_file(rpath)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,41 @@ async def test_put_files_async(
await fs._rm(remote_file_path)


def test_get_file_to_file_url():
register("memory")
fs: FsspecStore = fsspec.filesystem("memory")
fs.pipe_file("get_file_src.txt", b"hello world")

with TemporaryDirectory() as tmp:
local_file_path = Path(tmp) / "out.txt"
fs.get_file("get_file_src.txt", local_file_path.as_uri())
assert local_file_path.read_bytes() == b"hello world"


def test_get_file_to_windows_path():
register("memory")
fs: FsspecStore = fsspec.filesystem("memory")
lpath = r"C:\Users\me\out.txt"

with (
patch.object(fs, "_cat_file", return_value=b"hello world"),
patch.object(fs._local_store, "_isdir", return_value=False),
patch.object(fs._local_store, "_pipe_file") as mock_pipe_file,
):
fs.get_file("get_file_src.txt", lpath)

mock_pipe_file.assert_called_once_with(lpath, b"hello world")


def test_get_file_to_non_local_url_raises():
register("memory")
fs: FsspecStore = fsspec.filesystem("memory")
fs.pipe_file("get_file_src.txt", b"hello world")

with pytest.raises(ValueError, match="local path"):
fs.get_file("get_file_src.txt", "s3://bucket/out.txt")


@pytest.mark.network
def test_remote_parquet(minio_bucket: tuple[S3Config, ClientConfig]):
register(["https", "s3"])
Expand Down