google_cloud_storage/storage/
transport.rs

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//     https://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
15use crate::Result;
16use crate::model::{Object, ReadObjectRequest};
17use crate::model_ext::WriteObjectRequest;
18use crate::read_object::ReadObjectResponse;
19use crate::storage::client::StorageInner;
20use crate::storage::perform_upload::PerformUpload;
21use crate::storage::read_object::Reader;
22use crate::storage::request_options::RequestOptions;
23use crate::storage::streaming_source::{Seek, StreamingSource};
24use std::sync::Arc;
25
26/// An implementation of [`stub::Storage`][crate::storage::stub::Storage] that
27/// interacts with the Cloud Storage service.
28///
29/// This is the default implementation of a
30/// [`client::Storage<T>`][crate::storage::client::Storage].
31///
32/// ## Example
33///
34/// ```
35/// # async fn sample() -> anyhow::Result<()> {
36/// use google_cloud_storage::client::Storage;
37/// use google_cloud_storage::stub::DefaultStorage;
38/// let client: Storage<DefaultStorage> = Storage::builder().build().await?;
39/// # Ok(()) }
40/// ```
41#[derive(Clone, Debug)]
42pub struct Storage {
43    inner: Arc<StorageInner>,
44}
45
46impl Storage {
47    pub(crate) fn new(inner: Arc<StorageInner>) -> Arc<Self> {
48        Arc::new(Self { inner })
49    }
50}
51
52impl super::stub::Storage for Storage {
53    /// Implements [crate::client::Storage::read_object].
54    async fn read_object(
55        &self,
56        req: ReadObjectRequest,
57        options: RequestOptions,
58    ) -> Result<ReadObjectResponse> {
59        let reader = Reader {
60            inner: self.inner.clone(),
61            request: req,
62            options,
63        };
64        reader.response().await
65    }
66
67    /// Implements [crate::client::Storage::write_object].
68    async fn write_object_buffered<P>(
69        &self,
70        payload: P,
71        req: WriteObjectRequest,
72        options: RequestOptions,
73    ) -> Result<Object>
74    where
75        P: StreamingSource + Send + Sync + 'static,
76    {
77        PerformUpload::new(payload, self.inner.clone(), req.spec, req.params, options)
78            .send()
79            .await
80    }
81
82    /// Implements [crate::client::Storage::write_object].
83    async fn write_object_unbuffered<P>(
84        &self,
85        payload: P,
86        req: WriteObjectRequest,
87        options: RequestOptions,
88    ) -> Result<Object>
89    where
90        P: StreamingSource + Seek + Send + Sync + 'static,
91    {
92        PerformUpload::new(payload, self.inner.clone(), req.spec, req.params, options)
93            .send_unbuffered()
94            .await
95    }
96}