-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: read cloud creds for obstore from env #556
base: main
Are you sure you want to change the base?
Changes from all commits
7d5bc24
3e2f3e7
a0a4a87
fe6a4a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "stac-object-store-cache" | ||
description = "Create and cache object stores based on url in stac." | ||
version = "0.1.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
categories.workspace = true | ||
rust-version.workspace = true | ||
|
||
[features] | ||
object-store-aws = ["object_store/aws"] | ||
object-store-azure = ["object_store/azure"] | ||
object-store-gcp = ["object_store/gcp"] | ||
object-store-http = ["object_store/http"] | ||
object-store-all = [ | ||
"object-store-aws", | ||
"object-store-azure", | ||
"object-store-gcp", | ||
"object-store-http", | ||
] | ||
|
||
|
||
[dependencies] | ||
object_store = { workspace = true } | ||
once_cell = { workspace = true } | ||
thiserror.workspace = true | ||
tokio = { workspace = true } | ||
url = { workspace = true, features = ["serde"] } | ||
|
||
[dev-dependencies] | ||
tokio = { workspace = true, features = ["macros"] } | ||
tokio-test.workspace = true |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,247 @@ | ||||||||||
use std::{collections::HashMap, sync::Arc}; | ||||||||||
|
||||||||||
use crate::{Error, Result}; | ||||||||||
|
||||||||||
use object_store::{local::LocalFileSystem, path::Path, DynObjectStore, ObjectStoreScheme}; | ||||||||||
use once_cell::sync::Lazy; | ||||||||||
use tokio::sync::RwLock; | ||||||||||
use url::Url; | ||||||||||
|
||||||||||
// To avoid memory leaks, we clear the cache when it grows too big. | ||||||||||
// The value does not have any meaning, other than polars use the same. | ||||||||||
const CACHE_SIZE: usize = 8; | ||||||||||
|
||||||||||
static OBJECT_STORE_CACHE: Lazy<RwLock<HashMap<ObjectStoreIdentifier, Arc<DynObjectStore>>>> = | ||||||||||
Lazy::new(Default::default); | ||||||||||
|
||||||||||
/// Parameter set to identify and cache an object store | ||||||||||
#[derive(PartialEq, Eq, Hash, Debug)] | ||||||||||
struct ObjectStoreIdentifier { | ||||||||||
/// A base url to the bucket. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not always a bucket, right? I.e. Azure doesn't call them buckets. |
||||||||||
base_url: Url, | ||||||||||
|
||||||||||
/// Object Store options | ||||||||||
options: Vec<(String, String)>, | ||||||||||
} | ||||||||||
|
||||||||||
impl ObjectStoreIdentifier { | ||||||||||
fn new<I, K, V>(base_url: Url, options: I) -> Self | ||||||||||
where | ||||||||||
I: IntoIterator<Item = (K, V)>, | ||||||||||
K: AsRef<str>, | ||||||||||
V: Into<String>, | ||||||||||
{ | ||||||||||
Self { | ||||||||||
base_url, | ||||||||||
options: options | ||||||||||
.into_iter() | ||||||||||
.map(|(k, v)| (k.as_ref().into(), v.into())) | ||||||||||
.collect(), | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
fn get_options(&self) -> Vec<(String, String)> { | ||||||||||
self.options.to_owned() | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(any( | ||||||||||
feature = "object-store-aws", | ||||||||||
feature = "object-store-gcp", | ||||||||||
feature = "object-store-azure" | ||||||||||
))] | ||||||||||
macro_rules! builder_env_opts { | ||||||||||
($builder:ty, $url:expr, $options:expr) => {{ | ||||||||||
let builder = $options.into_iter().fold( | ||||||||||
<$builder>::from_env().with_url($url.to_string()), | ||||||||||
|builder, (key, value)| match key.as_ref().parse() { | ||||||||||
Ok(k) => builder.with_config(k, value), | ||||||||||
Err(_) => builder, | ||||||||||
}, | ||||||||||
); | ||||||||||
Arc::new(builder.build()?) | ||||||||||
}}; | ||||||||||
} | ||||||||||
|
||||||||||
/// This was yanked from [object_store::parse_url_opts] with the following changes: | ||||||||||
/// | ||||||||||
/// - Build [object_store::ObjectStore] with environment variables | ||||||||||
/// - Return [Arc] instead of [Box] | ||||||||||
#[cfg_attr( | ||||||||||
not(any( | ||||||||||
feature = "object-store-aws", | ||||||||||
feature = "object-store-gcp", | ||||||||||
feature = "object-store-azure", | ||||||||||
feature = "object-store-http" | ||||||||||
)), | ||||||||||
allow(unused_variables) | ||||||||||
)] | ||||||||||
fn create_object_store<I, K, V>( | ||||||||||
scheme: ObjectStoreScheme, | ||||||||||
url: &Url, | ||||||||||
options: I, | ||||||||||
) -> Result<Arc<DynObjectStore>> | ||||||||||
where | ||||||||||
I: IntoIterator<Item = (K, V)>, | ||||||||||
K: AsRef<str>, | ||||||||||
V: Into<String>, | ||||||||||
{ | ||||||||||
let store: Arc<DynObjectStore> = match scheme { | ||||||||||
ObjectStoreScheme::Local => Arc::new(LocalFileSystem::new()), | ||||||||||
#[cfg(feature = "object-store-aws")] | ||||||||||
ObjectStoreScheme::AmazonS3 => { | ||||||||||
builder_env_opts!(object_store::aws::AmazonS3Builder, url, options) | ||||||||||
} | ||||||||||
#[cfg(feature = "object-store-gcp")] | ||||||||||
ObjectStoreScheme::GoogleCloudStorage => { | ||||||||||
builder_env_opts!(object_store::gcp::GoogleCloudStorageBuilder, url, options) | ||||||||||
} | ||||||||||
#[cfg(feature = "object-store-azure")] | ||||||||||
ObjectStoreScheme::MicrosoftAzure => { | ||||||||||
builder_env_opts!(object_store::azure::MicrosoftAzureBuilder, url, options) | ||||||||||
} | ||||||||||
#[cfg(feature = "object-store-http")] | ||||||||||
ObjectStoreScheme::Http => { | ||||||||||
let url = &url[..url::Position::BeforePath]; | ||||||||||
let builder = options.into_iter().fold( | ||||||||||
object_store::http::HttpBuilder::new().with_url(url.to_string()), | ||||||||||
|builder, (key, value)| match key.as_ref().parse() { | ||||||||||
Ok(k) => builder.with_config(k, value), | ||||||||||
Err(_) => builder, | ||||||||||
}, | ||||||||||
); | ||||||||||
Arc::new(builder.build()?) | ||||||||||
} | ||||||||||
s => return Err(Error::ObjectStoreCreate { scheme: s }), | ||||||||||
}; | ||||||||||
Ok(store) | ||||||||||
} | ||||||||||
|
||||||||||
/// Drop-in replacement for [object_store::parse_url_opts] with caching and env vars. | ||||||||||
/// | ||||||||||
/// It will create or retrieve object store based on passed `url` and `options`. | ||||||||||
/// Keeps global cache | ||||||||||
pub async fn parse_url_opts<I, K, V>( | ||||||||||
url: &Url, | ||||||||||
options: I, | ||||||||||
) -> crate::Result<(Arc<DynObjectStore>, Path)> | ||||||||||
where | ||||||||||
I: IntoIterator<Item = (K, V)>, | ||||||||||
K: AsRef<str>, | ||||||||||
V: Into<String>, | ||||||||||
{ | ||||||||||
let (scheme, path) = ObjectStoreScheme::parse(url).map_err(object_store::Error::from)?; | ||||||||||
|
||||||||||
let base_url = url | ||||||||||
.as_ref() | ||||||||||
.strip_suffix(path.as_ref()) | ||||||||||
.unwrap_or_default() | ||||||||||
.try_into()?; | ||||||||||
|
||||||||||
let object_store_id = ObjectStoreIdentifier::new(base_url, options); | ||||||||||
let options = object_store_id.get_options(); | ||||||||||
|
||||||||||
{ | ||||||||||
let cache = OBJECT_STORE_CACHE.read().await; | ||||||||||
if let Some(store) = (*cache).get(&object_store_id) { | ||||||||||
return Ok((store.clone(), path)); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some |
||||||||||
} | ||||||||||
} | ||||||||||
let store = create_object_store(scheme, url, options)?; | ||||||||||
{ | ||||||||||
let mut cache = OBJECT_STORE_CACHE.write().await; | ||||||||||
|
||||||||||
if cache.len() >= CACHE_SIZE { | ||||||||||
(*cache).clear() | ||||||||||
} | ||||||||||
_ = (*cache).insert(object_store_id, store.clone()); | ||||||||||
} | ||||||||||
|
||||||||||
Ok((store.clone(), path)) | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(test)] | ||||||||||
mod tests { | ||||||||||
use url::Url; | ||||||||||
|
||||||||||
use super::*; | ||||||||||
|
||||||||||
#[tokio::test] | ||||||||||
async fn file_different_path() { | ||||||||||
let options: Vec<(String, String)> = Vec::new(); | ||||||||||
|
||||||||||
let url = Url::parse("file:///some/path").unwrap(); | ||||||||||
let (store, path) = parse_url_opts(&url, options.clone()).await.unwrap(); | ||||||||||
|
||||||||||
let url2 = Url::parse("file:///other/path").unwrap(); | ||||||||||
let (store2, _) = parse_url_opts(&url2, options.clone()).await.unwrap(); | ||||||||||
|
||||||||||
{ | ||||||||||
let cache = OBJECT_STORE_CACHE.read().await; | ||||||||||
println!("{cache:#?}") | ||||||||||
} | ||||||||||
|
||||||||||
Comment on lines
+179
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
assert!(Arc::ptr_eq(&store, &store2)); | ||||||||||
assert!(std::ptr::addr_eq(Arc::as_ptr(&store), Arc::as_ptr(&store2))); | ||||||||||
assert_eq!(path.as_ref(), "some/path"); | ||||||||||
} | ||||||||||
|
||||||||||
#[tokio::test] | ||||||||||
async fn file_different_options() { | ||||||||||
let options: Vec<(String, String)> = Vec::new(); | ||||||||||
|
||||||||||
let url = Url::parse("file:///some/path").unwrap(); | ||||||||||
let (store, _) = parse_url_opts(&url, options).await.unwrap(); | ||||||||||
|
||||||||||
let options2: Vec<(String, String)> = vec![(String::from("some"), String::from("option"))]; | ||||||||||
let url2 = Url::parse("file:///some/path").unwrap(); | ||||||||||
let (store2, _) = parse_url_opts(&url2, options2).await.unwrap(); | ||||||||||
|
||||||||||
assert!(!Arc::ptr_eq(&store, &store2)); | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(feature = "object-store-aws")] | ||||||||||
#[tokio::test] | ||||||||||
async fn cache_works() { | ||||||||||
let url = Url::parse("s3://bucket/item").unwrap(); | ||||||||||
let options: Vec<(String, String)> = Vec::new(); | ||||||||||
|
||||||||||
let (store1, path) = parse_url_opts(&url, options.clone()).await.unwrap(); | ||||||||||
|
||||||||||
let url2 = Url::parse("s3://bucket/item2").unwrap(); | ||||||||||
let (store2, _path) = parse_url_opts(&url2, options.clone()).await.unwrap(); | ||||||||||
|
||||||||||
assert!(Arc::ptr_eq(&store1, &store2)); | ||||||||||
assert_eq!(path.as_ref(), "item"); | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(feature = "object-store-aws")] | ||||||||||
#[tokio::test] | ||||||||||
async fn different_options() { | ||||||||||
let url = Url::parse("s3://bucket/item").unwrap(); | ||||||||||
let options: Vec<(String, String)> = Vec::new(); | ||||||||||
|
||||||||||
let (store, _path) = parse_url_opts(&url, options).await.unwrap(); | ||||||||||
|
||||||||||
let url2 = Url::parse("s3://bucket/item2").unwrap(); | ||||||||||
let options2: Vec<(String, String)> = vec![(String::from("some"), String::from("option"))]; | ||||||||||
let (store2, _path) = parse_url_opts(&url2, options2).await.unwrap(); | ||||||||||
|
||||||||||
assert!(!Arc::ptr_eq(&store, &store2)); | ||||||||||
} | ||||||||||
|
||||||||||
#[cfg(feature = "object-store-aws")] | ||||||||||
#[tokio::test] | ||||||||||
async fn different_urls() { | ||||||||||
let url = Url::parse("s3://bucket/item").unwrap(); | ||||||||||
let options: Vec<(String, String)> = Vec::new(); | ||||||||||
|
||||||||||
let (store, _path) = parse_url_opts(&url, options.clone()).await.unwrap(); | ||||||||||
|
||||||||||
let url2 = Url::parse("s3://other-bucket/item").unwrap(); | ||||||||||
// let options2: Vec<(String, String)> = vec![(String::from("some"), String::from("option"))]; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
let (store2, _path) = parse_url_opts(&url2, options).await.unwrap(); | ||||||||||
|
||||||||||
assert!(!Arc::ptr_eq(&store, &store2)); | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link to the polars code that uses it? Credit where credit is due.