Skip to content

Commit 1b58c8f

Browse files
authored
feat: pick up env in store (#751)
1 parent 134cf93 commit 1b58c8f

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

crates/io/src/store.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Format, Readable, Result, Writeable};
2-
use object_store::{ObjectStore, PutResult, path::Path};
2+
use object_store::{ObjectStore, ObjectStoreScheme, PutResult, path::Path};
33
use stac::Href;
44
use std::sync::Arc;
55

@@ -17,17 +17,62 @@ where
1717
K: AsRef<str>,
1818
V: Into<String>,
1919
{
20-
let (store, path) = match href.as_ref() {
21-
Href::Url(url) => object_store::parse_url_opts(url, options)?,
22-
Href::String(s) => {
23-
if s.starts_with("/") {
24-
object_store::parse_url_opts(&format!("file://{s}").parse()?, options)?
25-
} else {
26-
let s = std::env::current_dir()?.join(s);
27-
object_store::parse_url_opts(&format!("file://{}", s.display()).parse()?, options)?
20+
let parse = || -> Result<(Box<dyn ObjectStore>, Path)> {
21+
match href.as_ref() {
22+
Href::Url(url) => {
23+
tracing::debug!("parsing url={url}");
24+
// It's technically inefficient to parse it twice, but we're doing this to
25+
// then do IO so who cares.
26+
let (scheme, path) =
27+
ObjectStoreScheme::parse(url).map_err(object_store::Error::from)?;
28+
29+
#[cfg(feature = "store-aws")]
30+
if matches!(scheme, ObjectStoreScheme::AmazonS3) {
31+
let mut builder = object_store::aws::AmazonS3Builder::from_env();
32+
for (key, value) in options {
33+
builder = builder.with_config(key.as_ref().parse()?, value);
34+
}
35+
return Ok((Box::new(builder.with_url(url.to_string()).build()?), path));
36+
}
37+
38+
#[cfg(feature = "store-azure")]
39+
if matches!(scheme, ObjectStoreScheme::AmazonS3) {
40+
let mut builder = object_store::azure::MicrosoftAzureBuilder::from_env();
41+
for (key, value) in options {
42+
builder = builder.with_config(key.as_ref().parse()?, value);
43+
}
44+
return Ok((Box::new(builder.with_url(url.to_string()).build()?), path));
45+
}
46+
47+
#[cfg(feature = "store-gcp")]
48+
if matches!(scheme, ObjectStoreScheme::GoogleCloudStorage) {
49+
let mut builder = object_store::gcp::GoogleCloudStorageBuilder::from_env();
50+
for (key, value) in options {
51+
builder = builder.with_config(key.as_ref().parse()?, value);
52+
}
53+
return Ok((Box::new(builder.with_url(url.to_string()).build()?), path));
54+
}
55+
56+
let pair = object_store::parse_url_opts(url, options)?;
57+
Ok(pair)
58+
}
59+
Href::String(s) => {
60+
if s.starts_with("/") {
61+
let pair =
62+
object_store::parse_url_opts(&format!("file://{s}").parse()?, options)?;
63+
Ok(pair)
64+
} else {
65+
let s = std::env::current_dir()?.join(s);
66+
let pair = object_store::parse_url_opts(
67+
&format!("file://{}", s.display()).parse()?,
68+
options,
69+
)?;
70+
Ok(pair)
71+
}
2872
}
2973
}
3074
};
75+
let (store, path) = parse()?;
3176
Ok((store.into(), path))
3277
}
3378

0 commit comments

Comments
 (0)