-
Notifications
You must be signed in to change notification settings - Fork 849
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
/// 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()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
orhttps://andrew:[email protected]/path
and returned anArc<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
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.
This API is just a trait? It doesn't contain any parsing logic, nor any logic to interpret schemes directly
It isn't just schemes FWIW
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...
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.
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
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)