Skip to main content

snix_castore/object_store/
mod.rs

1//! Shared helpers for constructing [object_store::ObjectStore].
2
3use object_store::{ObjectStore, ObjectStoreScheme, path::Path};
4use url::Url;
5
6#[cfg(feature = "cloud")]
7mod aws;
8
9/// From a URL starting with objectstore+ in the scheme, trims query params and that prefix.
10pub fn trim_objectstore_prefix(
11    url: &url::Url,
12) -> Result<url::Url, Box<dyn std::error::Error + Send + Sync>> {
13    // We need to convert the URL to string, strip the prefix there, and then
14    // parse it back as url, as Url::set_scheme() rejects this transition.
15    let s = url.to_string();
16    let mut url = Url::parse(
17        s.strip_prefix("objectstore+")
18            .ok_or("Missing objectstore uri")?,
19    )?;
20    // trim the query pairs, they might contain credentials or local settings we don't want to send as-is.
21    url.set_query(None);
22
23    Ok(url)
24}
25
26/// Constructs an [ObjectStore] from a URL and additional object store options.
27///
28/// If the `cloud` feature is enabled and an S3 URL is passed,
29/// it uses [aws::setup_aws_object_store] for setup.
30/// This code honors the AWS credential chain, contrary to stock [object_store].
31///
32/// `opts` is the same as what [object_store::parse_url_opts] accepts.
33pub(crate) async fn setup_object_store<I, K, V>(
34    object_store_url: &Url,
35    opts: I,
36) -> Result<(Box<dyn ObjectStore>, Path), Box<dyn std::error::Error + Send + Sync>>
37where
38    I: IntoIterator<Item = (K, V)>,
39    K: AsRef<str>,
40    V: Into<String> + AsRef<str>,
41{
42    let (object_store_scheme, path) = ObjectStoreScheme::parse(object_store_url)?;
43
44    match object_store_scheme {
45        #[cfg(feature = "cloud")]
46        ObjectStoreScheme::AmazonS3 => {
47            // In the AWS case, we only support s3:// URLs.
48            if object_store_url.scheme() != "s3" {
49                return Err(Box::new(std::io::Error::new(
50                    std::io::ErrorKind::InvalidInput,
51                    "only s3://-style URLs supported",
52                )));
53            }
54
55            let store = aws::setup_aws_object_store(object_store_url, opts).await?;
56            Ok((Box::new(store) as Box<dyn ObjectStore>, path))
57        }
58        _ => Ok(object_store::parse_url_opts(object_store_url, opts)?),
59    }
60}