3636from google .cloud ._storage_v2 .types .storage import BidiWriteObjectRequest
3737
3838
39- from . import _utils
39+ from ._utils import raise_if_no_fast_crc32c
4040from google .cloud import _storage_v2
4141from google .cloud .storage ._experimental .asyncio .async_grpc_client import (
4242 AsyncGrpcClient ,
@@ -117,8 +117,8 @@ def __init__(
117117 client : AsyncGrpcClient .grpc_client ,
118118 bucket_name : str ,
119119 object_name : str ,
120- generation : Optional [ int ] = None ,
121- write_handle : Optional [ _storage_v2 . BidiWriteHandle ] = None ,
120+ generation = None ,
121+ write_handle = None ,
122122 writer_options : Optional [dict ] = None ,
123123 ):
124124 """
@@ -164,7 +164,7 @@ def __init__(
164164 :type object_name: str
165165 :param object_name: The name of the GCS Appendable Object to be written.
166166
167- :type generation: Optional[ int]
167+ :type generation: int
168168 :param generation: (Optional) If present, creates writer for that
169169 specific revision of that object. Use this to append data to an
170170 existing Appendable Object.
@@ -174,10 +174,10 @@ def __init__(
174174 overwriting existing objects).
175175
176176 Warning: If `None`, a new object is created. If an object with the
177- same name already exists, it will be overwritten the moment
177+ same name already exists, it will be overwritten the moment
178178 `writer.open()` is called.
179179
180- :type write_handle: _storage_v2.BidiWriteHandle
180+ :type write_handle: bytes
181181 :param write_handle: (Optional) An handle for writing the object.
182182 If provided, opening the bidi-gRPC connection will be faster.
183183
@@ -189,7 +189,7 @@ def __init__(
189189 servers. Default is `_DEFAULT_FLUSH_INTERVAL_BYTES`.
190190 Must be a multiple of `_MAX_CHUNK_SIZE_BYTES`.
191191 """
192- _utils . raise_if_no_fast_crc32c ()
192+ raise_if_no_fast_crc32c ()
193193 self .client = client
194194 self .bucket_name = bucket_name
195195 self .object_name = object_name
@@ -372,12 +372,13 @@ async def append(
372372 attempt_count = 0
373373
374374 def send_and_recv_generator (
375- requests_generator ,
375+ requests : List [ BidiWriteObjectRequest ] ,
376376 state : dict [str , _WriteState ],
377377 metadata : Optional [List [Tuple [str , str ]]] = None ,
378378 ):
379379 async def generator ():
380380 nonlocal attempt_count
381+ nonlocal requests
381382 attempt_count += 1
382383 resp = None
383384 async with self ._lock :
@@ -414,33 +415,16 @@ async def generator():
414415 write_state .bytes_sent = write_state .persisted_size
415416 write_state .bytes_since_last_flush = 0
416417
417- # Process requests from the generator
418- # Strategy handles state_lookup and flush on the last request,
419- # so we just stream requests directly
420- for chunk_req in requests_generator :
421- # Check if this is an open/state-lookup request (no checksummed_data)
422- if chunk_req .state_lookup and not chunk_req .checksummed_data :
423- # This is an open request - send it and get response
424- await self .write_obj_stream .send (chunk_req )
425- resp = await self .write_obj_stream .recv ()
426-
427- # Update state from open response
428- if resp :
429- if resp .persisted_size is not None :
430- self .persisted_size = resp .persisted_size
431- write_state .persisted_size = resp .persisted_size
432- self .offset = self .persisted_size
433- if resp .write_handle :
434- self .write_handle = resp .write_handle
435- write_state .write_handle = resp .write_handle
436- continue
437-
438- # This is a data request - send it
418+ requests = strategy .generate_requests (state )
419+
420+ num_requests = len (requests )
421+ for i , chunk_req in enumerate (requests ):
422+ if i == num_requests - 1 :
423+ chunk_req .state_lookup = True
424+ chunk_req .flush = True
439425 await self .write_obj_stream .send (chunk_req )
440426
441- # Get final response from the last request (which has state_lookup=True)
442427 resp = await self .write_obj_stream .recv ()
443-
444428 if resp :
445429 if resp .persisted_size is not None :
446430 self .persisted_size = resp .persisted_size
@@ -584,6 +568,7 @@ async def finalize(self) -> _storage_v2.Object:
584568 def is_stream_open (self ) -> bool :
585569 return self ._is_stream_open
586570
571+
587572 # helper methods.
588573 async def append_from_string (self , data : str ):
589574 """
0 commit comments