risingwave_object_store/object/opendal_engine/gcs.rs
1// Copyright 2025 RisingWave Labs
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
15use std::sync::Arc;
16
17use opendal::Operator;
18use opendal::layers::LoggingLayer;
19use opendal::services::Gcs;
20use risingwave_common::config::ObjectStoreConfig;
21
22use super::{MediaType, OpendalObjectStore};
23use crate::object::ObjectResult;
24use crate::object::object_metrics::ObjectStoreMetrics;
25
26impl OpendalObjectStore {
27 /// create opendal gcs engine.
28 pub fn new_gcs_engine(
29 bucket: String,
30 root: String,
31 config: Arc<ObjectStoreConfig>,
32 metrics: Arc<ObjectStoreMetrics>,
33 ) -> ObjectResult<Self> {
34 // Create gcs backend builder.
35 let mut builder = Gcs::default().bucket(&bucket).root(&root);
36
37 // if credential env is set, use it. Otherwise, ADC will be used.
38 let cred = std::env::var("GOOGLE_APPLICATION_CREDENTIALS");
39 if let Ok(cred) = cred {
40 builder = builder.credential(&cred);
41 }
42
43 let op: Operator = Operator::new(builder)?
44 .layer(LoggingLayer::default())
45 .finish();
46 Ok(Self {
47 op,
48 media_type: MediaType::Gcs,
49 config,
50 metrics,
51 })
52 }
53}