common_datasource/object_store/
gcs.rs1use 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}