common_datasource/object_store/
azblob.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::Azblob;
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 ACCOUNT_NAME: &str = "account_name";
26const ACCOUNT_KEY: &str = "account_key";
27const SAS_TOKEN: &str = "sas_token";
28
29pub fn is_supported_in_azblob(key: &str) -> bool {
30    [ENDPOINT, ACCOUNT_NAME, ACCOUNT_KEY, SAS_TOKEN].contains(&key)
31}
32
33pub fn build_azblob_backend(
34    container: &str,
35    root: &str,
36    connection: &HashMap<String, String>,
37) -> Result<ObjectStore> {
38    let mut builder = Azblob::default().root(root).container(container);
39
40    if let Some(account_name) = connection.get(ACCOUNT_NAME) {
41        builder = builder.account_name(account_name);
42    }
43
44    if let Some(account_key) = connection.get(ACCOUNT_KEY) {
45        builder = builder.account_key(account_key);
46    }
47
48    if let Some(endpoint) = connection.get(ENDPOINT) {
49        builder = builder.endpoint(endpoint);
50    }
51
52    if let Some(sas_token) = connection.get(SAS_TOKEN) {
53        builder = builder.sas_token(sas_token);
54    }
55
56    let object_store = ObjectStore::new(builder)
57        .context(error::BuildBackendSnafu)?
58        .finish();
59    Ok(with_instrument_layers(
60        with_retry_layers(object_store),
61        true,
62    ))
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_is_supported_in_azblob() {
71        assert!(is_supported_in_azblob(ENDPOINT));
72        assert!(is_supported_in_azblob(ACCOUNT_NAME));
73        assert!(is_supported_in_azblob(ACCOUNT_KEY));
74        assert!(is_supported_in_azblob(SAS_TOKEN));
75        assert!(!is_supported_in_azblob("foo"));
76    }
77}