common_datasource/object_store/
azblob.rs1use 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).context(error::BuildBackendSnafu)?;
57 Ok(with_instrument_layers(
58 with_retry_layers(object_store),
59 true,
60 ))
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_is_supported_in_azblob() {
69 assert!(is_supported_in_azblob(ENDPOINT));
70 assert!(is_supported_in_azblob(ACCOUNT_NAME));
71 assert!(is_supported_in_azblob(ACCOUNT_KEY));
72 assert!(is_supported_in_azblob(SAS_TOKEN));
73 assert!(!is_supported_in_azblob("foo"));
74 }
75}