Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
21e66a3
add AsyncAbstractObjectStream
chandra-siri Sep 17, 2025
39503f4
keep _AsyncAbstractObjectStream private
chandra-siri Sep 17, 2025
a161fd0
Add _AsyncReadObjectStream and it's stubs
chandra-siri Sep 17, 2025
dd862a2
complete __init__ for read_obj_str
chandra-siri Sep 17, 2025
aaabfd7
remove unuseful comments
chandra-siri Sep 17, 2025
23eea96
add methods open close send recv
chandra-siri Sep 17, 2025
71a7a79
change read_handle type from 'str' to 'bytes'
chandra-siri Sep 18, 2025
827aec0
feat: add async_multi_range_downloader
chandra-siri Sep 18, 2025
5be7469
fix: read_ranges should have buffer as well
chandra-siri Sep 18, 2025
b3ad551
rename MultiRangeDownloader to AsyncMultiRangeDownloader
chandra-siri Sep 18, 2025
a87f2be
feat: implement download_ranges method
chandra-siri Sep 18, 2025
c2e3c7b
add BytesIO in doc string
chandra-siri Sep 18, 2025
3c0fd66
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Sep 18, 2025
0810afc
fix doc strings, add licence and type hints
chandra-siri Sep 18, 2025
d43d889
Merge branch 'bidi_reads_1_abs_obj_stream' of github.com:googleapis/p…
chandra-siri Sep 18, 2025
a14bc68
pass abstract methods
chandra-siri Sep 18, 2025
fbacbb4
Merge branch 'bidi_reads_1_abs_obj_stream' of github.com:googleapis/p…
chandra-siri Sep 18, 2025
fd37489
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 18, 2025
635ad07
add handle param
chandra-siri Sep 18, 2025
ba453d4
include handle in tests
chandra-siri Sep 18, 2025
800c6df
remove unit tests for abstract class
chandra-siri Sep 18, 2025
8b40812
Merge branch 'bidi_reads_1_abs_obj_stream' of github.com:googleapis/p…
chandra-siri Sep 18, 2025
18529ad
edit doc string for _AsyncReadObjectStream
chandra-siri Sep 18, 2025
df2532e
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 18, 2025
b4da1ac
refactor unit tests for async_read_object_stream
chandra-siri Sep 18, 2025
6dec6c6
bucket_name and object_name cannot be NONE
chandra-siri Sep 18, 2025
90a65f6
Merge branch 'bidi_reads_1_abs_obj_stream' of github.com:googleapis/p…
chandra-siri Sep 18, 2025
a154905
bucket_name and object_name cannot be None
chandra-siri Sep 18, 2025
d69cd63
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 18, 2025
20afca4
Merge branch 'bidi_reads_3_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
14a2aba
simplyfy tests for open
chandra-siri Sep 19, 2025
078afca
simply tests for send recv and close
chandra-siri Sep 19, 2025
2054989
minor edit - add bidi-stream in doc string
chandra-siri Sep 19, 2025
2b9ae2e
Merge branch 'bidi_reads_1_abs_obj_stream' of github.com:googleapis/p…
chandra-siri Sep 19, 2025
c06896c
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
916e7f1
Merge branch 'bidi_reads_3_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
3658502
Merge branch 'bidi_reads_4_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
8366f0b
simplify unit tests
chandra-siri Sep 19, 2025
4a774ec
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
0991383
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Sep 19, 2025
f589b89
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
06b102c
improve doc string
chandra-siri Sep 19, 2025
52494b4
fix unit tess in MRD
chandra-siri Sep 19, 2025
8e00701
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
0e60694
add checks for invalid inputs
chandra-siri Sep 19, 2025
961def8
Merge branch 'bidi_reads_2_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 19, 2025
e9d0f9e
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Sep 22, 2025
c4f61ea
Merge branch 'bidi_reads_3_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 22, 2025
ef5d917
Merge branch 'bidi_reads_4_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 22, 2025
7fbf28c
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 22, 2025
dcb6a55
remove duplicated import
chandra-siri Sep 23, 2025
90d8597
remove unused import
chandra-siri Sep 23, 2025
229887b
Merge branch 'bidi_reads_3_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 23, 2025
5a6ffc7
Merge branch 'bidi_reads_4_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 23, 2025
9f15551
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 23, 2025
234336f
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Sep 23, 2025
521154c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 23, 2025
8338ab2
fix unit test
chandra-siri Sep 23, 2025
c3cb076
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 23, 2025
ac8e651
remove unused import
chandra-siri Sep 23, 2025
c6b1098
Merge branch 'bidi_reads_5_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 23, 2025
6469063
implement basic functionality for download_ranges
chandra-siri Sep 24, 2025
17404af
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Sep 24, 2025
130046f
doc string for further testcase
chandra-siri Sep 24, 2025
0c61e87
remove unwanted comments
chandra-siri Sep 24, 2025
027758c
remove testing code
chandra-siri Sep 24, 2025
570983f
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Sep 24, 2025
99ffe2d
fix doc strings
chandra-siri Sep 24, 2025
e571dd1
Merge branch 'bidi_reads_6_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 24, 2025
962b7b9
instantiate `socket_like_rpc` while opening
chandra-siri Sep 24, 2025
fdaf1ae
update doc string to describe `read_ranges` format
chandra-siri Sep 25, 2025
cf6e004
add is_stream_open and check this before "open" "close" "send" "recv"
chandra-siri Sep 25, 2025
153007d
Merge branch 'bidi_reads_6_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 25, 2025
f1874ed
assert stream is indeed closed/open on close/open
chandra-siri Sep 25, 2025
64de09f
fix linting issues for assert statements
chandra-siri Sep 25, 2025
112cb68
add test case for ranges > 1000
chandra-siri Sep 26, 2025
b56efd9
don't return exception object , raise instead
chandra-siri Sep 29, 2025
890eac1
don't return exception object , raise instead
chandra-siri Sep 29, 2025
84b63f1
correct doc string rtype
chandra-siri Sep 29, 2025
f93b25e
Merge branch 'bidi_reads_6_read_obj_stream' of github.com:googleapis/…
chandra-siri Sep 29, 2025
3e3b0fa
Merge branch 'main' into bidi_reads_7
chandra-siri Sep 30, 2025
f551fd9
minor edit in doc string.
chandra-siri Sep 30, 2025
ae4d31e
Merge branch 'bidi_reads_7' of github.com:googleapis/python-storage i…
chandra-siri Sep 30, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,41 @@ def __init__(
self.rpc = self.client._client._transport._wrapped_methods[
self.client._client._transport.bidi_read_object
]
first_bidi_read_req = _storage_v2.BidiReadObjectRequest(
self.first_bidi_read_req = _storage_v2.BidiReadObjectRequest(
read_object_spec=_storage_v2.BidiReadObjectSpec(
bucket=self._full_bucket_name, object=object_name
),
)
self.metadata = (("x-goog-request-params", f"bucket={self._full_bucket_name}"),)
self.socket_like_rpc = AsyncBidiRpc(
self.rpc, initial_request=first_bidi_read_req, metadata=self.metadata
)
self.socket_like_rpc: Optional[AsyncBidiRpc] = None
self._is_stream_open: bool = False

async def open(self) -> None:
"""Opens the bidi-gRPC connection to read from the object.

This method sends an initial request to start the stream and receives
the first response containing metadata and a read handle.
"""
if self._is_stream_open:
raise ValueError("Stream is already open")
self.socket_like_rpc = AsyncBidiRpc(
self.rpc, initial_request=self.first_bidi_read_req, metadata=self.metadata
)
await self.socket_like_rpc.open() # this is actually 1 send
response = await self.socket_like_rpc.recv()
if self.generation_number is None:
self.generation_number = response.metadata.generation

self.read_handle = response.read_handle

self._is_stream_open = True

async def close(self) -> None:
"""Closes the bidi-gRPC connection."""
if not self._is_stream_open:
raise ValueError("Stream is not open")
await self.socket_like_rpc.close()
self._is_stream_open = False
Comment thread
Pulkit0110 marked this conversation as resolved.

async def send(
self, bidi_read_object_request: _storage_v2.BidiReadObjectRequest
Expand All @@ -120,6 +129,8 @@ async def send(
The request message to send. This is typically used to specify
the read offset and limit.
"""
if not self._is_stream_open:
raise ValueError("Stream is not open")
await self.socket_like_rpc.send(bidi_read_object_request)

async def recv(self) -> _storage_v2.BidiReadObjectResponse:
Expand All @@ -132,4 +143,10 @@ async def recv(self) -> _storage_v2.BidiReadObjectResponse:
:class:`~google.cloud._storage_v2.types.BidiReadObjectResponse`:
The response message from the server.
"""
if not self._is_stream_open:
raise ValueError("Stream is not open")
return await self.socket_like_rpc.recv()

@property
def is_stream_open(self) -> bool:
return self._is_stream_open
190 changes: 154 additions & 36 deletions tests/unit/asyncio/test_async_read_object_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,42 @@
_TEST_READ_HANDLE = b"test-read-handle"


async def instantiate_read_obj_stream(mock_client, mock_cls_async_bidi_rpc, open=True):
"""Helper to create an instance of _AsyncReadObjectStream and open it by default."""
socket_like_rpc = AsyncMock()
mock_cls_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = AsyncMock()

recv_response = mock.MagicMock(spec=_storage_v2.BidiReadObjectResponse)
recv_response.metadata = mock.MagicMock(spec=_storage_v2.Object)
recv_response.metadata.generation = _TEST_GENERATION_NUMBER
recv_response.read_handle = _TEST_READ_HANDLE
socket_like_rpc.recv = AsyncMock(return_value=recv_response)

read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
)

if open:
await read_obj_stream.open()

return read_obj_stream


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
def test_init_with_bucket_object_generation(mock_client, mock_async_bidi_rpc):
# Arrange
rpc_sentinel = mock.sentinel.A
mock_client._client._transport.bidi_read_object = "bidi_read_object_rpc"
mock_client._client._transport._wrapped_methods = {
"bidi_read_object_rpc": mock.sentinel.A
"bidi_read_object_rpc": rpc_sentinel,
}
full_bucket_name = f"projects/_/buckets/{_TEST_BUCKET_NAME}"
first_bidi_read_req = _storage_v2.BidiReadObjectRequest(
Expand All @@ -45,39 +71,36 @@ def test_init_with_bucket_object_generation(mock_client, mock_async_bidi_rpc):
),
)

# Act
read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
generation_number=_TEST_GENERATION_NUMBER,
read_handle=_TEST_READ_HANDLE,
)

mock_async_bidi_rpc.assert_called_once_with(
mock.sentinel.A,
initial_request=first_bidi_read_req,
metadata=(("x-goog-request-params", f"bucket={full_bucket_name}"),),
)
assert read_obj_stream.socket_like_rpc is mock_async_bidi_rpc.return_value
# Assert
assert read_obj_stream.bucket_name == _TEST_BUCKET_NAME
assert read_obj_stream.object_name == _TEST_OBJECT_NAME
assert read_obj_stream.generation_number == _TEST_GENERATION_NUMBER
assert read_obj_stream.read_handle == _TEST_READ_HANDLE
assert read_obj_stream.first_bidi_read_req == first_bidi_read_req
assert read_obj_stream.rpc == rpc_sentinel


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_open(mock_client):
async def test_open(mock_client, mock_cls_async_bidi_rpc):
# arrange
read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)
read_obj_stream.socket_like_rpc.open = AsyncMock()

recv_response = mock.MagicMock(spec=_storage_v2.BidiReadObjectResponse)
recv_response.metadata = mock.MagicMock(spec=_storage_v2.Object)
recv_response.metadata.generation = _TEST_GENERATION_NUMBER
recv_response.read_handle = _TEST_READ_HANDLE
read_obj_stream.socket_like_rpc.recv = AsyncMock(return_value=recv_response)

# act
await read_obj_stream.open()
Expand All @@ -88,40 +111,88 @@ async def test_open(mock_client):

assert read_obj_stream.generation_number == _TEST_GENERATION_NUMBER
assert read_obj_stream.read_handle == _TEST_READ_HANDLE
assert read_obj_stream.is_stream_open


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_close(mock_client):
async def test_open_when_already_open_should_raise_error(
mock_client, mock_cls_async_bidi_rpc
):
# arrange
read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)

# act + assert (pythonic)
with pytest.raises(ValueError) as exc:
await read_obj_stream.open()

# assert
assert str(exc.value) == "Stream is already open"


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_close(mock_client, mock_cls_async_bidi_rpc):
# arrange
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
read_obj_stream.socket_like_rpc.close = AsyncMock()

# act
await read_obj_stream.close()

# assert
read_obj_stream.socket_like_rpc.close.assert_called_once()
assert not read_obj_stream.is_stream_open


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_send(mock_client):
async def test_close_without_open_should_raise_error(
mock_client, mock_cls_async_bidi_rpc
):
# arrange
read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)

# act + assert (pythonic)
with pytest.raises(ValueError) as exc:
await read_obj_stream.close()

# assert
assert str(exc.value) == "Stream is not open"


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_send(mock_client, mock_cls_async_bidi_rpc):
# arrange
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
read_obj_stream.socket_like_rpc.send = AsyncMock()

# act
bidi_read_object_request = _storage_v2.BidiReadObjectRequest()
Expand All @@ -133,16 +204,40 @@ async def test_send(mock_client):
)


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_recv(mock_client):
async def test_send_without_open_should_raise_error(
mock_client, mock_cls_async_bidi_rpc
):
# arrange
read_obj_stream = _AsyncReadObjectStream(
client=mock_client,
bucket_name=_TEST_BUCKET_NAME,
object_name=_TEST_OBJECT_NAME,
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)

# act + assert (pythonic)
with pytest.raises(ValueError) as exc:
await read_obj_stream.send(_storage_v2.BidiReadObjectRequest())

# assert
assert str(exc.value) == "Stream is not open"


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_recv(mock_client, mock_cls_async_bidi_rpc):
# arrange
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
bidi_read_object_response = _storage_v2.BidiReadObjectResponse()
read_obj_stream.socket_like_rpc.recv = AsyncMock(
Expand All @@ -155,3 +250,26 @@ async def test_recv(mock_client):
# assert
read_obj_stream.socket_like_rpc.recv.assert_called_once()
assert response == bidi_read_object_response


@mock.patch(
"google.cloud.storage._experimental.asyncio.async_read_object_stream.AsyncBidiRpc"
)
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_recv_without_open_should_raise_error(
mock_client, mock_cls_async_bidi_rpc
):
# arrange
read_obj_stream = await instantiate_read_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)

# act + assert (pythonic)
with pytest.raises(ValueError) as exc:
await read_obj_stream.recv()

# assert
assert str(exc.value) == "Stream is not open"