Skip to content
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

Add ObjectStoreScheme (#4047) #4184

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions object_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,11 @@ pub use client::{backoff::BackoffConfig, retry::RetryConfig};

#[cfg(any(feature = "azure", feature = "aws", feature = "gcp"))]
mod multipart;
mod scheme;
mod util;

pub use scheme::ObjectStoreScheme;

use crate::path::Path;
#[cfg(not(target_arch = "wasm32"))]
use crate::util::maybe_spawn_blocking;
Expand Down
130 changes: 130 additions & 0 deletions object_store/src/scheme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::{Error, Result};
use url::Url;

/// Recognises various URL formats, identifying the relevant [`ObjectStore`](crate::ObjectStore)
///
/// This can be combined with the [with_url](crate::aws::AmazonS3Builder::with_url) methods
/// on the corresponding builder to construct the relevant type of store
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum ObjectStoreScheme {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand why you are proposing to add this to the object store crate.

Users of object_store would still have to match on the resulting scheme and instantiate a builder / configuration appropriate to whatever they wanted. The extra value to having a hard coded list of url prefixes seems relatively minimal.

Maybe this is just a first step.

If I were a user I would want something that took a url like s3://foo-bucket or https://andrew:[email protected]/path and returned an Arc<dyn ObjectStore> .

For convenience the object_store crate could have default interpretations of these urls, but also some way to extend the API;

Basically I think the API here makes a lot of sense https://docs.rs/datafusion/latest/datafusion/datasource/object_store/trait.ObjectStoreRegistry.html

Copy link
Contributor Author

@tustvold tustvold May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the API here makes a lot of sense

This API is just a trait? It doesn't contain any parsing logic, nor any logic to interpret schemes directly

The extra value to having a hard coded list of url prefixes seems relatively minimal

It isn't just schemes FWIW

why you are proposing to add this to the object store crate
If I were a user I would want something that took a url like s3://foo-bucket or https://andrew:[email protected]/path and returned an Arc

As explained in the description, because there isn't a way I can see to make such an API that is both coherent and not extremely opinionated...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API is just a trait? It doesn't contain any parsing logic, nor any logic to interpret schemes directly

The API is a trait but DataFusion provides default parsing logic / scheme interpretation in https://docs.rs/datafusion/latest/datafusion/datasource/object_store/struct.DefaultObjectStoreRegistry.html

As explained in the description, because there isn't a way I can see to make such an API that is both coherent and not extremely opinionated...

I wast trying to suggest an API that let users implement their own opinions while also providing a default implementation that worked for simple cases (with whatever opinions you wanted)

/// Url corresponding to [`LocalFileSystem`](crate::local::LocalFileSystem)
Local,
/// Url corresponding to [`InMemory`](crate::memory::InMemory)
Memory,
/// Url corresponding to [`AmazonS3`](crate::aws::AmazonS3)
AmazonS3,
/// Url corresponding to [`GoogleCloudStorage`](crate::gcp::GoogleCloudStorage)
GoogleCloudStorage,
/// Url corresponding to [`MicrosoftAzure`](crate::azure::MicrosoftAzure)
MicrosoftAzure,
/// Url corresponding to [`HttpStore`](crate::http::HttpStore)
Http,
}

impl ObjectStoreScheme {
/// Create an [`ObjectStoreScheme`] from the provided [`Url`]
pub fn try_new(url: &Url) -> Result<Self> {
match (url.scheme(), url.host_str()) {
("file", None) => Ok(Self::Local),
("memory", None) => Ok(Self::Memory),
("s3" | "s3a", Some(_)) => Ok(Self::AmazonS3),
("gs", Some(_)) => Ok(Self::GoogleCloudStorage),
("az" | "adl" | "azure" | "abfs" | "abfss", Some(_)) => {
Ok(Self::MicrosoftAzure)
}
("http", Some(_)) => Ok(Self::Http),
("https", Some(host)) => {
if host.ends_with("dfs.core.windows.net")
|| host.ends_with("blob.core.windows.net")
{
Ok(Self::MicrosoftAzure)
} else if host.ends_with("amazonaws.com") {
Ok(Self::AmazonS3)
} else {
Ok(Self::Http)
}
}
_ => Err(Error::Generic {
store: "ObjectStoreScheme",
source: format!("Unrecognized URL: {url}").into(),
}),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_scheme() {
let cases = [
("file:/path", ObjectStoreScheme::Local),
("file:///path", ObjectStoreScheme::Local),
("memory:/foo", ObjectStoreScheme::Memory),
("memory:///", ObjectStoreScheme::Memory),
("s3://bucket/path", ObjectStoreScheme::AmazonS3),
("s3a://bucket/path", ObjectStoreScheme::AmazonS3),
(
"https://s3.bucket.amazonaws.com",
ObjectStoreScheme::AmazonS3,
),
(
"https://bucket.s3.region.amazonaws.com",
ObjectStoreScheme::AmazonS3,
),
("abfs://container/path", ObjectStoreScheme::MicrosoftAzure),
(
"abfs://file_system@account_name.dfs.core.windows.net/path",
ObjectStoreScheme::MicrosoftAzure,
),
(
"abfss://file_system@account_name.dfs.core.windows.net/path",
ObjectStoreScheme::MicrosoftAzure,
),
(
"https://account.dfs.core.windows.net",
ObjectStoreScheme::MicrosoftAzure,
),
(
"https://account.blob.core.windows.net",
ObjectStoreScheme::MicrosoftAzure,
),
("gs://bucket/path", ObjectStoreScheme::GoogleCloudStorage),
("http://mydomain/path", ObjectStoreScheme::Http),
("https://mydomain/path", ObjectStoreScheme::Http),
];

for (s, expected) in cases {
let url = Url::parse(s).unwrap();
assert_eq!(ObjectStoreScheme::try_new(&url).unwrap(), expected);
}

let neg_cases = [
"unix:/run/foo.socket",
"file://remote/path",
"memory://remote/",
];
for s in neg_cases {
let url = Url::parse(s).unwrap();
assert!(ObjectStoreScheme::try_new(&url).is_err());
}
}
}