Skip to content

Commit 0c21210

Browse files
authored
fix: filter download_kwargs in BlobReader (#1411)
1 parent c2a2ce5 commit 0c21210

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

google/cloud/storage/fileio.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class BlobReader(io.BufferedIOBase):
8989
configuration changes for Retry objects such as delays and deadlines
9090
are respected.
9191
92+
:type download_kwargs: dict
9293
:param download_kwargs:
9394
Keyword arguments to pass to the underlying API calls.
9495
The following arguments are supported:
@@ -98,9 +99,10 @@ class BlobReader(io.BufferedIOBase):
9899
- ``if_metageneration_match``
99100
- ``if_metageneration_not_match``
100101
- ``timeout``
102+
- ``raw_download``
101103
102-
Note that download_kwargs are also applied to blob.reload(), if a reload
103-
is needed during seek().
104+
Note that download_kwargs (excluding ``raw_download``) are also applied to blob.reload(),
105+
if a reload is needed during seek().
104106
"""
105107

106108
def __init__(self, blob, chunk_size=None, retry=DEFAULT_RETRY, **download_kwargs):
@@ -175,7 +177,10 @@ def seek(self, pos, whence=0):
175177
self._checkClosed() # Raises ValueError if closed.
176178

177179
if self._blob.size is None:
178-
self._blob.reload(**self._download_kwargs)
180+
reload_kwargs = {
181+
k: v for k, v in self._download_kwargs.items() if k != "raw_download"
182+
}
183+
self._blob.reload(**reload_kwargs)
179184

180185
initial_offset = self._pos + self._buffer.tell()
181186

@@ -272,6 +277,7 @@ class BlobWriter(io.BufferedIOBase):
272277
configuration changes for Retry objects such as delays and deadlines
273278
are respected.
274279
280+
:type upload_kwargs: dict
275281
:param upload_kwargs:
276282
Keyword arguments to pass to the underlying API
277283
calls. The following arguments are supported:

tests/system/test_fileio.py

+23
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,26 @@ def test_blobwriter_exit(
116116
blobs_to_delete.append(blob)
117117
# blob should have been uploaded
118118
assert blob.exists()
119+
120+
121+
def test_blobreader_w_raw_download(
122+
shared_bucket,
123+
blobs_to_delete,
124+
file_data,
125+
):
126+
blob = shared_bucket.blob("LargeFile")
127+
info = file_data["big"]
128+
with open(info["path"], "rb") as file_obj:
129+
with blob.open("wb", chunk_size=256 * 1024, if_generation_match=0) as writer:
130+
writer.write(file_obj.read())
131+
blobs_to_delete.append(blob)
132+
133+
# Test BlobReader read and seek handles raw downloads.
134+
with open(info["path"], "rb") as file_obj:
135+
with blob.open("rb", chunk_size=256 * 1024, raw_download=True) as reader:
136+
reader.seek(0)
137+
file_obj.seek(0)
138+
assert file_obj.read() == reader.read()
139+
# End of file reached; further reads should be blank but not
140+
# raise an error.
141+
assert reader.read() == b""

0 commit comments

Comments
 (0)