common_datasource/object_store/
gcs.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 std::collections::HashMap;
16
17use object_store::ObjectStore;
18use object_store::services::Gcs;
19use object_store::util::{with_instrument_layers, with_retry_layers};
20use snafu::ResultExt;
21
22use crate::error::{self, Result};
23
24const ENDPOINT: &str = "endpoint";
25const CREDENTIAL: &str = "credential";
26const SCOPE: &str = "scope";
27
28pub fn is_supported_in_gcs(key: &str) -> bool {
29    [ENDPOINT, CREDENTIAL, SCOPE].contains(&key)
30}
31
32pub fn build_gcs_backend(
33    bucket: &str,
34    root: &str,
35    connection: &HashMap<String, String>,
36) -> Result<ObjectStore> {
37    let mut builder = Gcs::default().root(root).bucket(bucket);
38
39    if let Some(scope) = connection.get(SCOPE) {
40        builder = builder.scope(scope);
41    }
42
43    if let Some(credential) = connection.get(CREDENTIAL) {
44        builder = builder.credential(credential);
45    }
46
47    if let Some(endpoint) = connection.get(ENDPOINT) {
48        builder = builder.endpoint(endpoint);
49    }
50
51    let object_store = ObjectStore::new(builder)
52        .context(error::BuildBackendSnafu)?
53        .finish();
54    Ok(with_instrument_layers(
55        with_retry_layers(object_store),
56        true,
57    ))
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_is_supported_in_gcs() {
66        assert!(is_supported_in_gcs(ENDPOINT));
67        assert!(is_supported_in_gcs(CREDENTIAL));
68        assert!(is_supported_in_gcs(SCOPE));
69        assert!(!is_supported_in_gcs("foo"));
70    }
71}