You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Read ranges with one request and open files in smaller blocks - #456
Reading a few bytes of a Drive file downloaded up to 100 MiB first.
Example
fs.head("root/data/model.bin", 1024) # first 1 KiB of a 2 GiB filefs.cat_file("root/data/model.bin", 0, 16)
withfs.open("root/data/model.bin") asf:
magic=f.read(8)
Before:
head() downloaded the first 100 MiB and returned 1 KiB of it.
cat_file(path, start, end) with a start failed, because the download stream cannot seek. tail() failed the same way.
open() downloaded 100 MiB per read request.
The 100 MiB is the Google client library's default chunk size. It came with the streaming reader in #39 (2020); no reason was given for using it.
After
head(), tail() and cat_file(path, start, end) send one request with an HTTP Range. head(path, 1024) downloads 1 KiB.
Negative positions count from the end, as in a slice. A range with a negative end is read from its start to the end of the file and cut locally, so only a range from the end to a position from the start needs the file's size.
A range past the end returns b"", and so does tail(path, 0) or a negative size, without a download.
A Google Workspace file (a Doc, a Sheet) can only be exported whole, so it is exported and then cut with the caller's bounds. Drive's size for such a file is not the length of its export.
A range of a folder raises IsADirectoryError, and of a missing file ID FileNotFoundError.
With compression=, or any other option that changes the bytes, the read goes through fsspec as before.
open() asks Drive for fs.blocksize bytes per request: 4 MiB by default, as in other fsspec filesystems. fs.open(path, block_size=...) still sets it per file. A program that reads large files from start to end makes 25 times more requests than with 100 MiB; the guide suggests a larger block_size, such as 64 MiB, for it.
get() / get_file(), which download whole files, keep their 100 MiB chunks for now. A smaller chunk means more requests; I want a measurement on real Drive before changing that.
Validation
Live, on real Drive, with one 2 MiB file of random bytes that the read tests share:
head(path, 10) returns the first 10 bytes, and the client asks for bytes=0-9 and receives 10 bytes;
tail(path, 10) asks for bytes=-10 and receives 10 bytes;
cat_file returns the same bytes as a slice of the data, for a range in the middle, one counted from the end, one to the end, and one past the end;
tail(path, 0) and tail(path, -5) return b"" and download nothing;
head() of an empty file is b"";
cat_file(path, end=5, compression="gzip") returns the first 5 uncompressed bytes;
cat_file also returns the slice for a range from the start to before the end, and from the end to a position;
a range of a Google Doc, from the start, before the end, at the end and from the end to a position, equals that slice of its export (real Drive gave a new Doc a size that made the first version return b"" for the range before the end);
a range of a folder raises IsADirectoryError, by path and by folder ID;
head() of the ID of a deleted file raises FileNotFoundError (a made-up ID can get 400 from Drive instead of 404);
open().read(10) asks for one block: 4 MiB with no block_size or "default", 512 KiB with block_size=512 * 1024.
Against the local fake Drive, which honours ranges like Drive, the first version of this PR failed the empty-tail, compression, folder and missing-ID cases. The Doc case needs the size real Drive reports. The offline test of the old 100 MiB default is replaced by the live block test.
The fsspec guide describes the read sizes and the reads that download more than they return. The offline suite passes with the current google-auth and with 2.15.0 (534 passed).
- head(), tail() and cat_file(path, start, end) ask Drive for the bytes
they return, with one ranged request. head() used to download the
first 100 MiB of a file, and cat_file() with a start failed because
the stream cannot seek. A Google Workspace file is exported whole
and cut, since Drive exports only whole files.
- open() asks Drive for fs.blocksize bytes (4 MiB by default) per
read, instead of 100 MiB, unless block_size says otherwise.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
[AI generated — Copilot]
5-pass review summary: strong improvement for ranged reads and short open() reads, but has real edge-case bugs (tail(path, 0) downloads the entire file, missing _translate_not_found, silent b"" on directories with negative ranges, unchunked open-ended cat_file(path, start=N)), plus a 7x test speedup opportunity (two_mib_file scope).
…s as asked
tail() of no bytes returns b"" without a download. A range of a folder
raises IsADirectoryError, and of a missing file ID FileNotFoundError. A
read with compression goes through fsspec, as before. A Google
Workspace export is sliced with the caller's bounds, since Drive's size
for such a file is not the export's length. One lookup gives the file's
ID and size.
The live read tests share one 2 MiB file, and check the default
download block size without changing it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Workspace file reads normalize bounds against the reported size rather than the export length, causing mixed negative/positive ranges to return empty data instead of the expected bytes.
A range that ends before the end of the file is read from its start or
from the end and cut locally, so only a range from the end to a
position from the start needs the size. Drive's size of a Google
Workspace file is not the length of its export: real Drive gave a new
Doc a size that made cat_file(doc, -3, -1) return b"". A file without
a checksum, which is how Drive marks Workspace files and shortcuts, has
no usable size and is exported whole.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drive answers some made-up IDs with 400 Bad Request instead of 404, so the
test now uses the ID of a file it deleted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Empty-range reads bypass raw-ID root validation, masking missing-file and directory errors, and the documentation overstates exact-download guarantees for negative bounds and compressed reads.
Validate raw-ID roots before returning empty ranges
pydrive2/fs/spec.py:1443
An empty range bypasses validation when the filesystem is rooted at a raw file ID: _file_for_range(..., need_size=False) returns the ID without metadata, so head(folder_id, 0) and cat_file(missing_id, 0, 0) return b"" instead of IsADirectoryError or FileNotFoundError. Validate the raw-ID root before this early return, without downloading content.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reading a few bytes of a Drive file downloaded up to 100 MiB first.
Example
Before:
head()downloaded the first 100 MiB and returned 1 KiB of it.cat_file(path, start, end)with a start failed, because the download stream cannot seek.tail()failed the same way.open()downloaded 100 MiB per read request.The 100 MiB is the Google client library's default chunk size. It came with the streaming reader in #39 (2020); no reason was given for using it.
After
head(),tail()andcat_file(path, start, end)send one request with an HTTPRange.head(path, 1024)downloads 1 KiB.b"", and so doestail(path, 0)or a negative size, without a download.IsADirectoryError, and of a missing file IDFileNotFoundError.compression=, or any other option that changes the bytes, the read goes through fsspec as before.open()asks Drive forfs.blocksizebytes per request: 4 MiB by default, as in other fsspec filesystems.fs.open(path, block_size=...)still sets it per file. A program that reads large files from start to end makes 25 times more requests than with 100 MiB; the guide suggests a largerblock_size, such as 64 MiB, for it.get()/get_file(), which download whole files, keep their 100 MiB chunks for now. A smaller chunk means more requests; I want a measurement on real Drive before changing that.Validation
Live, on real Drive, with one 2 MiB file of random bytes that the read tests share:
head(path, 10)returns the first 10 bytes, and the client asks forbytes=0-9and receives 10 bytes;tail(path, 10)asks forbytes=-10and receives 10 bytes;cat_filereturns the same bytes as a slice of the data, for a range in the middle, one counted from the end, one to the end, and one past the end;tail(path, 0)andtail(path, -5)returnb""and download nothing;head()of an empty file isb"";cat_file(path, end=5, compression="gzip")returns the first 5 uncompressed bytes;cat_filealso returns the slice for a range from the start to before the end, and from the end to a position;b""for the range before the end);IsADirectoryError, by path and by folder ID;head()of the ID of a deleted file raisesFileNotFoundError(a made-up ID can get 400 from Drive instead of 404);open().read(10)asks for one block: 4 MiB with noblock_sizeor"default", 512 KiB withblock_size=512 * 1024.Against the local fake Drive, which honours ranges like Drive, the first version of this PR failed the empty-tail, compression, folder and missing-ID cases. The Doc case needs the size real Drive reports. The offline test of the old 100 MiB default is replaced by the live block test.
The fsspec guide describes the read sizes and the reads that download more than they return. The offline suite passes with the current google-auth and with 2.15.0 (534 passed).
🤖 Generated with Claude Code