fsspec-rs filesystems¶
fsspec-rs provides Rust-backed filesystems through fsspec classes.
Importing fsspec_rs registers the protocols with fsspec:
Filesystem |
Protocols |
Class |
|---|---|---|
Local filesystem |
|
|
In memory |
|
|
Amazon S3 |
|
|
The standard file, local, memory, and s3 registrations remain unchanged.
import fsspec
import fsspec_rs
with fsspec.open("file-rs:///tmp/example.txt", "rb") as f:
data = f.read()
Local filesystem¶
LocalFileSystem wraps the Rust LocalFs backend, which uses std::fs for
local paths. It is marked as a local filesystem (local_file = True) and
implements fsspec’s core filesystem operations.
from fsspec_rs import LocalFileSystem
fs = LocalFileSystem(auto_mkdir=True)
fs.pipe_file("/tmp/fsspec-rs/example.txt", b"hello")
assert fs.exists("/tmp/fsspec-rs/example.txt")
assert fs.isfile("/tmp/fsspec-rs/example.txt")
print(fs.info("/tmp/fsspec-rs/example.txt"))
Local paths¶
Direct method calls use normal local paths:
fs.cat_file("/tmp/fsspec-rs/example.txt")
fsspec URLs use file-rs:// or local-rs://:
with fsspec.open("local-rs:///tmp/fsspec-rs/example.txt", "rb") as f:
print(f.read())
LocalFileSystem._strip_protocol() removes only the Rust-specific local
protocols. The file:// and local:// protocols still select fsspec’s Python
implementation.
Opening files¶
Supported modes are rb, wb, ab, and xb, plus the aliases
r, w, a, and x at the Rust layer. Python wrappers expose binary file
objects that implement read, write, seek, tell, flush, close,
readable, writable, and seekable.
with fs.open("/tmp/fsspec-rs/out.bin", "wb") as f:
f.write(b"payload")
with fs.open("/tmp/fsspec-rs/out.bin", "rb") as f:
f.seek(2)
print(f.read())
auto_mkdir=True creates missing parent directories for write, append,
exclusive-create, and copy operations. With the default auto_mkdir=False,
the operating system reports an error if a parent directory is missing.
wb and xb files are buffered in memory and written when the file is closed.
The contents go to a temporary file next to the target, which is synced and
then renamed over the target, so other readers see either the previous contents
or the complete new contents. flush() does not make partial contents visible.
xb fails at close if another writer created the file after it was opened.
ab files append to the target directly.
Local operations¶
Local filesystem methods delegated to Rust include:
Listing and metadata:
ls,info,exists,isdir,isfile,sizeReads and writes:
open,cat_file,pipe_file,head,tail,read_text,write_text,touchDirectory operations:
mkdir,makedirs,rmdir,walk,find,duMutation and transfer:
rm_file,rm,cp_file,copy,mv,get_file,put_file
The cache_type, block_size, and max_blocks options are accepted by
open() for API compatibility but do not change local read behavior. Local
reads use direct filesystem operations.
In memory¶
MemoryFileSystem wraps the Rust MemoryFs backend, which keeps files in
process memory. Every instance shares one store, so a file written through one
MemoryFileSystem is visible through all of them until the process exits.
import fsspec
from fsspec_rs import MemoryFileSystem
fs = MemoryFileSystem()
fs.pipe_file("/data/example.txt", b"hello")
with fsspec.open("memory-rs://data/example.txt", "rb") as f:
print(f.read())
Paths are rooted at /. Writing a file needs no mkdir: directories exist while
a file below them does, as in fsspec’s MemoryFileSystem. mkdir creates an
empty directory that persists until rmdir or rm. Written files become visible
when they are closed, so readers never see partial contents.
The store is separate from fsspec’s Python memory://. In Rust, and to C, C++,
and other extensions through libfsspec, the same store is memory://.
S3¶
S3FileSystem wraps the Rust S3Fs backend, which uses Apache Arrow’s
object_store crate and runs its async S3 calls through an embedded Tokio
runtime. The Python protocol is s3-rs, so it can coexist with s3fs.
from fsspec_rs import S3FileSystem
fs = S3FileSystem(bucket="my-bucket", region="us-east-1")
fs.pipe_file("reports/hello.txt", b"hello")
print(fs.cat_file("reports/hello.txt"))
S3 paths¶
S3FileSystem is constructed for one bucket. Method calls can use keys within
that bucket:
fs.ls("reports")
fs.info("reports/hello.txt")
fsspec URLs include the bucket. _get_kwargs_from_urls() extracts constructor
options from the URL:
with fsspec.open(
"s3-rs://my-bucket/reports/hello.txt",
"rb",
region="us-east-1",
) as f:
print(f.read())
Object names are stored internally without a leading slash. Directory
entries are S3 prefixes, not real directories; mkdir() and rmdir() are
no-ops.
Credentials and endpoints¶
Credential resolution follows this order:
Explicit constructor arguments:
key,secret,token,region,endpoint_url, andanonfsspec.config.conf["s3"], including values populated from fsspec config environment variablesobject_store’s own AWS credential chain, such as environment variables, profiles, and instance or task roles
client_kwargs={"endpoint_url": ...} is accepted for compatibility with s3fs.
The Rust backend allows plain HTTP when the endpoint starts with http://,
as used by local S3-compatible services.
fs = S3FileSystem(
bucket="benchmark",
key="minioadmin",
secret="minioadmin",
endpoint_url="http://localhost:9000",
region="us-east-1",
)
Anonymous access is available with anon=True.
Caching reads¶
S3 reads support optional Rust-side cache strategies through cache_type:
|
Behavior |
|---|---|
|
Fetch each requested byte range directly. |
|
Keep one lookahead window for sequential reads. |
|
Cache fixed-size blocks with LRU eviction. |
|
Fetch the whole object on first read. |
with fs.open(
"large/table.parquet",
"rb",
cache_type="block",
block_size=8 * 1024 * 1024,
) as f:
header = f.read(4096)
block_size defaults to 4 MiB, and the Rust max_blocks default is 32. The
Python file wrapper does not forward max_blocks from open(), so callers
cannot change it through that method.
Without an explicit cache_type, S3 read mode eagerly loads the whole object
into memory. Explicit readahead, block, and none select range-based reads.
S3 operations¶
S3 methods delegated to Rust include:
Listing and metadata:
ls,info,exists,isdir,isfile,sizeReads and writes:
open,cat_file,pipe_file,head,tail,read_textPrefix traversal:
walk,findObject mutation:
rm_file,rm,cp_file
S3 writes buffer object contents in memory before upload. The public API does not support concurrent multipart uploads or signed URLs.