Skip to content

Commit 53878f6

Browse files
authored
feat: get aws creds from env (#642)
- Closes #414
1 parent 598b254 commit 53878f6

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

crates/core/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ required-features = ["validate"]
8787
name = "migrate"
8888
required-features = ["validate"]
8989

90+
[[test]]
91+
name = "aws"
92+
required-features = ["object-store-aws"]
93+
9094
[package.metadata.docs.rs]
9195
all-features = true
9296
rustdoc-args = ["--cfg", "docsrs"]

crates/core/src/format.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl Format {
160160
RealizedHref::Url(url) => {
161161
use object_store::ObjectStore;
162162

163-
let (object_store, path) = object_store::parse_url_opts(&url, options)?;
163+
let (object_store, path) = parse_url_opts(&url, options)?;
164164
let get_result = object_store.get(&path).await?;
165165
let mut value: T = self.from_bytes(get_result.bytes().await?)?;
166166
*value.self_href_mut() = Some(Href::Url(url));
@@ -241,7 +241,7 @@ impl Format {
241241
if let Ok(url) = url::Url::parse(&href) {
242242
use object_store::ObjectStore;
243243

244-
let (object_store, path) = object_store::parse_url_opts(&url, options)?;
244+
let (object_store, path) = parse_url_opts(&url, options)?;
245245
let bytes = self.into_vec(value)?;
246246
let put_result = object_store.put(&path, bytes.into()).await?;
247247
Ok(Some(put_result))
@@ -267,6 +267,33 @@ impl Format {
267267
}
268268
}
269269

270+
#[cfg(feature = "object-store")]
271+
fn parse_url_opts<I, K, V>(
272+
url: &url::Url,
273+
options: I,
274+
) -> Result<(Box<dyn object_store::ObjectStore>, object_store::path::Path)>
275+
where
276+
I: IntoIterator<Item = (K, V)>,
277+
K: AsRef<str>,
278+
V: Into<String>,
279+
{
280+
use object_store::ObjectStoreScheme;
281+
282+
// It's technically inefficient to parse it twice, but we're doing this to
283+
// then do IO so who cares.
284+
#[cfg(feature = "object-store-aws")]
285+
if let Ok((ObjectStoreScheme::AmazonS3, path)) = ObjectStoreScheme::parse(url) {
286+
let mut builder = object_store::aws::AmazonS3Builder::from_env();
287+
for (key, value) in options {
288+
builder = builder.with_config(key.as_ref().parse()?, value);
289+
}
290+
return Ok((Box::new(builder.with_url(url.to_string()).build()?), path));
291+
}
292+
293+
let result = object_store::parse_url_opts(url, options)?;
294+
Ok(result)
295+
}
296+
270297
impl Default for Format {
271298
fn default() -> Self {
272299
Self::Json(false)

crates/core/tests/aws.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[test]
2+
fn read_from_s3() {
3+
tokio_test::block_on(async {
4+
stac::io::get_opts::<stac::Catalog, _, _, _>(
5+
"s3://nz-elevation/catalog.json",
6+
[("skip_signature", "true"), ("region", "ap-southeast-2")],
7+
)
8+
.await
9+
.unwrap();
10+
});
11+
}

crates/core/tests/examples.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,3 @@ fn v1_1_0(#[files("../../spec-examples/v1.1.0/**/*.json")] path: PathBuf) {
1313
let value: Value = stac::read(path.to_str().unwrap()).unwrap();
1414
value.validate().unwrap();
1515
}
16-
17-
#[test]
18-
#[cfg(feature = "object-store-aws")]
19-
fn read_from_s3() {
20-
tokio_test::block_on(async {
21-
stac::io::get_opts::<stac::Catalog, _, _, _>(
22-
"s3://nz-elevation/catalog.json",
23-
[("skip_signature", "true"), ("region", "ap-southeast-2")],
24-
)
25-
.await
26-
.unwrap();
27-
});
28-
}

0 commit comments

Comments
 (0)