datanode/store/
s3.rs

1// Copyright 2023 Greptime Team
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 common_base::secrets::ExposeSecret;
16use common_telemetry::info;
17use object_store::services::S3;
18use object_store::{util, ObjectStore};
19use snafu::prelude::*;
20
21use crate::config::S3Config;
22use crate::error::{self, Result};
23use crate::store::build_http_client;
24
25pub(crate) async fn new_s3_object_store(s3_config: &S3Config) -> Result<ObjectStore> {
26    let root = util::normalize_dir(&s3_config.root);
27
28    info!(
29        "The s3 storage bucket is: {}, root is: {}",
30        s3_config.bucket, &root
31    );
32
33    let client = build_http_client(&s3_config.http_client)?;
34
35    let mut builder = S3::default()
36        .root(&root)
37        .bucket(&s3_config.bucket)
38        .access_key_id(s3_config.access_key_id.expose_secret())
39        .secret_access_key(s3_config.secret_access_key.expose_secret())
40        .http_client(client);
41
42    if s3_config.endpoint.is_some() {
43        builder = builder.endpoint(s3_config.endpoint.as_ref().unwrap());
44    }
45    if s3_config.region.is_some() {
46        builder = builder.region(s3_config.region.as_ref().unwrap());
47    }
48    if s3_config.enable_virtual_host_style {
49        builder = builder.enable_virtual_host_style();
50    }
51
52    Ok(ObjectStore::new(builder)
53        .context(error::InitBackendSnafu)?
54        .finish())
55}