|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +NOTE: |
| 16 | +This is _experimental module for upcoming support for Rapid Storage. |
| 17 | +(https://cloud.google.com/blog/products/storage-data-transfer/high-performance-storage-innovations-for-ai-hpc#:~:text=your%20AI%20workloads%3A-,Rapid%20Storage,-%3A%20A%20new) |
| 18 | +
|
| 19 | +APIs may not work as intended and are not stable yet. Feature is not |
| 20 | +GA(Generally Available) yet, please contact your TAM(Technical Account Manager) |
| 21 | +if you want to use these Rapid Storage APIs. |
| 22 | +
|
| 23 | +""" |
| 24 | +from typing import Optional |
| 25 | +from google.cloud import _storage_v2 |
| 26 | +from google.cloud.storage._experimental.asyncio.async_grpc_client import AsyncGrpcClient |
| 27 | +from google.cloud.storage._experimental.asyncio.async_abstract_object_stream import ( |
| 28 | + _AsyncAbstractObjectStream, |
| 29 | +) |
| 30 | +from google.api_core.bidi_async import AsyncBidiRpc |
| 31 | + |
| 32 | + |
| 33 | +class _AsyncWriteObjectStream(_AsyncAbstractObjectStream): |
| 34 | + """Class representing a gRPC bidi-stream for writing data from a GCS |
| 35 | + ``Appendable Object``. |
| 36 | +
|
| 37 | + This class provides a unix socket-like interface to a GCS ``Object``, with |
| 38 | + methods like ``open``, ``close``, ``send``, and ``recv``. |
| 39 | +
|
| 40 | + :type client: :class:`~google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client` |
| 41 | + :param client: async grpc client to use for making API requests. |
| 42 | +
|
| 43 | + :type bucket_name: str |
| 44 | + :param bucket_name: The name of the GCS ``bucket`` containing the object. |
| 45 | +
|
| 46 | + :type object_name: str |
| 47 | + :param object_name: The name of the GCS ``Appendable Object`` to be write. |
| 48 | +
|
| 49 | + :type generation_number: int |
| 50 | + :param generation_number: (Optional) If present, selects a specific revision of |
| 51 | + this object. If None, a new object is created. |
| 52 | +
|
| 53 | + :type write_handle: bytes |
| 54 | + :param write_handle: (Optional) An existing handle for writing the object. |
| 55 | + If provided, opening the bidi-gRPC connection will be faster. |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + client: AsyncGrpcClient.grpc_client, |
| 61 | + bucket_name: str, |
| 62 | + object_name: str, |
| 63 | + generation_number: Optional[int] = None, # None means new object |
| 64 | + write_handle: Optional[bytes] = None, |
| 65 | + ) -> None: |
| 66 | + if client is None: |
| 67 | + raise ValueError("client must be provided") |
| 68 | + if bucket_name is None: |
| 69 | + raise ValueError("bucket_name must be provided") |
| 70 | + if object_name is None: |
| 71 | + raise ValueError("object_name must be provided") |
| 72 | + |
| 73 | + super().__init__( |
| 74 | + bucket_name=bucket_name, |
| 75 | + object_name=object_name, |
| 76 | + generation_number=generation_number, |
| 77 | + ) |
| 78 | + self.client: AsyncGrpcClient.grpc_client = client |
| 79 | + self.write_handle: Optional[bytes] = write_handle |
| 80 | + |
| 81 | + self._full_bucket_name = f"projects/_/buckets/{self.bucket_name}" |
| 82 | + |
| 83 | + self.rpc = self.client._client._transport._wrapped_methods[ |
| 84 | + self.client._client._transport.bidi_write_object |
| 85 | + ] |
| 86 | + |
| 87 | + self.metadata = (("x-goog-request-params", f"bucket={self._full_bucket_name}"),) |
| 88 | + self.socket_like_rpc: Optional[AsyncBidiRpc] = None |
| 89 | + self._is_stream_open: bool = False |
| 90 | + self.first_bidi_write_req = None |
| 91 | + self.persisted_size = 0 |
| 92 | + self.object_resource: Optional[_storage_v2.Object] = None |
| 93 | + |
| 94 | + async def open(self) -> None: |
| 95 | + """Opening an object for write , should do it's state lookup |
| 96 | + to know what's the persisted size is. |
| 97 | + """ |
| 98 | + raise NotImplementedError( |
| 99 | + "open() is not implemented yet in _AsyncWriteObjectStream" |
| 100 | + ) |
| 101 | + |
| 102 | + async def close(self) -> None: |
| 103 | + """Closes the bidi-gRPC connection.""" |
| 104 | + raise NotImplementedError( |
| 105 | + "close() is not implemented yet in _AsyncWriteObjectStream" |
| 106 | + ) |
| 107 | + |
| 108 | + async def send( |
| 109 | + self, bidi_write_object_request: _storage_v2.BidiWriteObjectRequest |
| 110 | + ) -> None: |
| 111 | + """Sends a request message on the stream. |
| 112 | +
|
| 113 | + Args: |
| 114 | + bidi_write_object_request (:class:`~google.cloud._storage_v2.types.BidiReadObjectRequest`): |
| 115 | + The request message to send. This is typically used to specify |
| 116 | + the read offset and limit. |
| 117 | + """ |
| 118 | + raise NotImplementedError( |
| 119 | + "send() is not implemented yet in _AsyncWriteObjectStream" |
| 120 | + ) |
| 121 | + |
| 122 | + async def recv(self) -> _storage_v2.BidiWriteObjectResponse: |
| 123 | + """Receives a response from the stream. |
| 124 | +
|
| 125 | + This method waits for the next message from the server, which could |
| 126 | + contain object data or metadata. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + :class:`~google.cloud._storage_v2.types.BidiWriteObjectResponse`: |
| 130 | + The response message from the server. |
| 131 | + """ |
| 132 | + raise NotImplementedError( |
| 133 | + "recv() is not implemented yet in _AsyncWriteObjectStream" |
| 134 | + ) |
0 commit comments