sova-storage 0.1.4

Object storage for Sova (local / memory / S3 / GCS / Azure)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! OpenDAL-backed [`BlobStore`] (S3 / GCS / Azure).
//!
//! Env builders are split so unit tests can feed a fake env map without mutating
//! process environment (and without hitting the network).

use crate::{normalize_key, normalize_prefix, BlobStore, BoxFuture, PutOpts, StorageError};
use bytes::Bytes;
use opendal::Operator;
use std::time::Duration;

#[derive(Clone)]
pub struct OpendalStore {
    op: Operator,
}

impl OpendalStore {
    pub fn new(op: Operator) -> Self {
        Self { op }
    }
}

impl BlobStore for OpendalStore {
    fn put<'a>(
        &'a self,
        key: &'a str,
        data: Bytes,
        opts: PutOpts,
    ) -> BoxFuture<'a, Result<(), StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            let rich = opts.content_type.is_some()
                || opts.content_disposition.is_some()
                || opts.cache_control.is_some()
                || !opts.metadata.is_empty();
            if !rich {
                self.op.write(&key, data.to_vec()).await?;
                return Ok(());
            }
            let mut w = self.op.write_with(&key, data.to_vec());
            if let Some(ct) = opts.content_type.as_deref() {
                w = w.content_type(ct);
            }
            if let Some(cd) = opts.content_disposition.as_deref() {
                w = w.content_disposition(cd);
            }
            if let Some(cc) = opts.cache_control.as_deref() {
                w = w.cache_control(cc);
            }
            if !opts.metadata.is_empty() {
                w = w.user_metadata(opts.metadata);
            }
            w.await?;
            Ok(())
        })
    }

    fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Bytes>, StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            match self.op.read(&key).await {
                Ok(buf) => Ok(Some(Bytes::copy_from_slice(buf.to_bytes().as_ref()))),
                Err(e) if e.kind() == opendal::ErrorKind::NotFound => Ok(None),
                Err(e) => Err(e.into()),
            }
        })
    }

    fn delete<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<(), StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            match self.op.delete(&key).await {
                Ok(()) => Ok(()),
                Err(e) if e.kind() == opendal::ErrorKind::NotFound => Ok(()),
                Err(e) => Err(e.into()),
            }
        })
    }

    fn exists<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<bool, StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            match self.op.stat(&key).await {
                Ok(_) => Ok(true),
                Err(e) if e.kind() == opendal::ErrorKind::NotFound => Ok(false),
                Err(e) => Err(e.into()),
            }
        })
    }

    fn list<'a>(&'a self, prefix: &'a str) -> BoxFuture<'a, Result<Vec<String>, StorageError>> {
        Box::pin(async move {
            let prefix = normalize_prefix(prefix)?;
            let path = if prefix.is_empty() {
                "/".to_string()
            } else if prefix.ends_with('/') {
                prefix.clone()
            } else {
                format!("{prefix}/")
            };
            let entries = self.op.list(&path).await?;
            let mut keys = Vec::new();
            for entry in entries {
                if entry.metadata().is_file() {
                    let p = entry.path().trim_start_matches('/').to_string();
                    if !p.is_empty() {
                        keys.push(p);
                    }
                }
            }
            // Exact key match when prefix is a file path
            if !prefix.is_empty()
                && !prefix.ends_with('/')
                && self.exists(&prefix).await?
                && !keys.iter().any(|k| k == &prefix)
            {
                keys.push(prefix);
            }
            keys.sort();
            keys.dedup();
            Ok(keys)
        })
    }

    fn temporary_url<'a>(
        &'a self,
        key: &'a str,
        expires: Duration,
    ) -> BoxFuture<'a, Result<String, StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            let req = self.op.presign_read(&key, expires).await?;
            Ok(req.uri().to_string())
        })
    }

    fn temporary_upload_url<'a>(
        &'a self,
        key: &'a str,
        expires: Duration,
    ) -> BoxFuture<'a, Result<String, StorageError>> {
        Box::pin(async move {
            let key = normalize_key(key)?;
            let req = self.op.presign_write(&key, expires).await?;
            Ok(req.uri().to_string())
        })
    }
}

fn env_get(key: &str) -> Option<String> {
    std::env::var(key).ok().filter(|s| !s.is_empty())
}

fn env_truthy(raw: &str) -> bool {
    matches!(
        raw.trim().to_ascii_lowercase().as_str(),
        "1" | "true" | "yes" | "on"
    )
}

fn env_falsy(raw: &str) -> bool {
    matches!(
        raw.trim().to_ascii_lowercase().as_str(),
        "0" | "false" | "no" | "off"
    )
}

/// Parsed S3/R2/MinIO settings from env (testable without OpenDAL I/O).
#[cfg(feature = "s3")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct S3EnvConfig {
    pub bucket: String,
    pub region: String,
    pub endpoint: Option<String>,
    pub access_key_id: Option<String>,
    pub secret_access_key: Option<String>,
    pub session_token: Option<String>,
    pub root: Option<String>,
    /// Path-style addressing (MinIO/custom endpoints). When `false`, virtual-host style.
    pub force_path_style: bool,
}

#[cfg(feature = "s3")]
impl S3EnvConfig {
    /// Read process env (`SOVA_STORAGE_*` / `AWS_*`).
    pub fn from_env() -> Result<Self, StorageError> {
        Self::from_vars(env_get)
    }

    /// Build from a lookup fn (unit tests pass a `HashMap` closure).
    pub fn from_vars<F>(mut get: F) -> Result<Self, StorageError>
    where
        F: FnMut(&str) -> Option<String>,
    {
        let bucket = get("SOVA_STORAGE_BUCKET")
            .or_else(|| get("AWS_BUCKET"))
            .filter(|s| !s.is_empty())
            .ok_or_else(|| {
                StorageError::Msg(
                    "SOVA_STORAGE_BUCKET (or AWS_BUCKET) is required for s3".into(),
                )
            })?;

        let endpoint = get("SOVA_STORAGE_ENDPOINT").filter(|s| !s.is_empty());

        let region = get("SOVA_STORAGE_REGION")
            .or_else(|| get("AWS_REGION"))
            .filter(|s| !s.is_empty())
            .or_else(|| endpoint.as_ref().map(|_| "auto".to_string()))
            .ok_or_else(|| {
                StorageError::Msg(
                    "SOVA_STORAGE_REGION or AWS_REGION is required for s3 \
                     (or set SOVA_STORAGE_ENDPOINT for MinIO/R2 to default region=auto)"
                        .into(),
                )
            })?;

        // Default: path-style when a custom endpoint is set (MinIO); virtual-host for plain AWS.
        let force_path_style = match get("SOVA_STORAGE_FORCE_PATH_STYLE") {
            Some(raw) if env_truthy(&raw) => true,
            Some(raw) if env_falsy(&raw) => false,
            Some(raw) => {
                return Err(StorageError::Msg(format!(
                    "invalid SOVA_STORAGE_FORCE_PATH_STYLE={raw} (use 1/0, true/false)"
                )));
            }
            None => endpoint.is_some(),
        };

        Ok(Self {
            bucket,
            region,
            endpoint,
            access_key_id: get("AWS_ACCESS_KEY_ID").filter(|s| !s.is_empty()),
            secret_access_key: get("AWS_SECRET_ACCESS_KEY").filter(|s| !s.is_empty()),
            session_token: get("AWS_SESSION_TOKEN")
                .or_else(|| get("SOVA_STORAGE_SESSION_TOKEN"))
                .filter(|s| !s.is_empty()),
            root: get("SOVA_STORAGE_ROOT").filter(|s| !s.is_empty()),
            force_path_style,
        })
    }

    /// Build an OpenDAL S3 operator from this config.
    pub fn build(self) -> Result<OpendalStore, StorageError> {
        use opendal::services::S3;

        let mut builder = S3::default()
            .bucket(&self.bucket)
            .region(&self.region);

        if let Some(endpoint) = self.endpoint.as_deref() {
            builder = builder.endpoint(endpoint);
        }
        if let Some(root) = self.root.as_deref() {
            builder = builder.root(root);
        }
        if let Some(key) = self.access_key_id.as_deref() {
            builder = builder.access_key_id(key);
        }
        if let Some(secret) = self.secret_access_key.as_deref() {
            builder = builder.secret_access_key(secret);
        }
        if let Some(token) = self.session_token.as_deref() {
            builder = builder.session_token(token);
        }
        // OpenDAL defaults to path-style; enable virtual-host when path-style is off.
        if !self.force_path_style {
            builder = builder.enable_virtual_host_style();
        }

        let op = Operator::new(builder)?;
        Ok(OpendalStore::new(op))
    }
}

/// S3 / R2 / MinIO from env. See [`S3EnvConfig`].
#[cfg(feature = "s3")]
pub fn s3_from_env() -> Result<OpendalStore, StorageError> {
    S3EnvConfig::from_env()?.build()
}

#[cfg(feature = "gcs")]
pub fn gcs_from_env() -> Result<OpendalStore, StorageError> {
    use opendal::services::Gcs;

    let bucket = env_get("SOVA_STORAGE_BUCKET").ok_or_else(|| {
        StorageError::Msg("SOVA_STORAGE_BUCKET is required for gcs".into())
    })?;

    let mut builder = Gcs::default().bucket(&bucket);
    if let Some(cred) = env_get("GOOGLE_APPLICATION_CREDENTIALS") {
        builder = builder.credential_path(&cred);
    }
    if let Some(root) = env_get("SOVA_STORAGE_ROOT") {
        builder = builder.root(&root);
    }
    if let Some(endpoint) = env_get("SOVA_STORAGE_ENDPOINT") {
        builder = builder.endpoint(&endpoint);
    }

    let op = Operator::new(builder)?;
    Ok(OpendalStore::new(op))
}

#[cfg(feature = "azure")]
pub fn azure_from_env() -> Result<OpendalStore, StorageError> {
    use opendal::services::Azblob;

    let container = env_get("SOVA_STORAGE_CONTAINER")
        .or_else(|| env_get("SOVA_STORAGE_BUCKET"))
        .ok_or_else(|| {
            StorageError::Msg(
                "SOVA_STORAGE_CONTAINER (or SOVA_STORAGE_BUCKET) required for azure".into(),
            )
        })?;

    let mut builder = Azblob::default().container(&container);
    if let Some(account) = env_get("AZURE_STORAGE_ACCOUNT_NAME") {
        builder = builder.account_name(&account);
    }
    if let Some(key) = env_get("AZURE_STORAGE_ACCOUNT_KEY") {
        builder = builder.account_key(&key);
    }
    // Azurite / custom: AZURE_STORAGE_ENDPOINT preferred, else SOVA_STORAGE_ENDPOINT.
    if let Some(endpoint) =
        env_get("AZURE_STORAGE_ENDPOINT").or_else(|| env_get("SOVA_STORAGE_ENDPOINT"))
    {
        builder = builder.endpoint(&endpoint);
    }
    if let Some(root) = env_get("SOVA_STORAGE_ROOT") {
        builder = builder.root(&root);
    }

    let op = Operator::new(builder)?;
    Ok(OpendalStore::new(op))
}

#[cfg(all(test, feature = "s3"))]
mod s3_env_tests {
    use super::*;
    use std::collections::HashMap;

    fn map_get<'a>(
        map: &'a HashMap<&'static str, &'static str>,
    ) -> impl FnMut(&str) -> Option<String> + 'a {
        move |k| map.get(k).map(|s| (*s).to_string())
    }

    #[test]
    fn minio_endpoint_defaults_region_auto_and_path_style() {
        let mut env = HashMap::new();
        env.insert("SOVA_STORAGE_BUCKET", "demo");
        env.insert("SOVA_STORAGE_ENDPOINT", "http://127.0.0.1:9000");
        env.insert("AWS_ACCESS_KEY_ID", "minio");
        env.insert("AWS_SECRET_ACCESS_KEY", "minio123");

        let cfg = S3EnvConfig::from_vars(map_get(&env)).unwrap();
        assert_eq!(cfg.region, "auto");
        assert!(cfg.force_path_style);
        assert_eq!(cfg.endpoint.as_deref(), Some("http://127.0.0.1:9000"));
    }

    #[test]
    fn aws_requires_region_without_endpoint() {
        let mut env = HashMap::new();
        env.insert("SOVA_STORAGE_BUCKET", "demo");
        env.insert("AWS_ACCESS_KEY_ID", "ak");
        env.insert("AWS_SECRET_ACCESS_KEY", "sk");

        let err = S3EnvConfig::from_vars(map_get(&env)).unwrap_err();
        assert!(err.to_string().contains("REGION"));
    }

    #[test]
    fn aws_virtual_host_when_region_set() {
        let mut env = HashMap::new();
        env.insert("SOVA_STORAGE_BUCKET", "demo");
        env.insert("AWS_REGION", "eu-west-1");

        let cfg = S3EnvConfig::from_vars(map_get(&env)).unwrap();
        assert_eq!(cfg.region, "eu-west-1");
        assert!(!cfg.force_path_style);
    }

    #[test]
    fn force_path_style_override_and_session_token() {
        let mut env = HashMap::new();
        env.insert("SOVA_STORAGE_BUCKET", "demo");
        env.insert("AWS_REGION", "us-east-1");
        env.insert("SOVA_STORAGE_FORCE_PATH_STYLE", "1");
        env.insert("AWS_SESSION_TOKEN", "tok");
        env.insert("SOVA_STORAGE_ROOT", "uploads");

        let cfg = S3EnvConfig::from_vars(map_get(&env)).unwrap();
        assert!(cfg.force_path_style);
        assert_eq!(cfg.session_token.as_deref(), Some("tok"));
        assert_eq!(cfg.root.as_deref(), Some("uploads"));
    }

    #[test]
    fn empty_bucket_rejected() {
        let mut env = HashMap::new();
        env.insert("SOVA_STORAGE_BUCKET", "");
        env.insert("AWS_REGION", "us-east-1");
        let err = S3EnvConfig::from_vars(map_get(&env)).unwrap_err();
        assert!(err.to_string().contains("BUCKET"));
    }
}