Skip to content

Commit

Permalink
feat: add FsOptions::parse to new DynFs
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Sep 29, 2024
1 parent 5682847 commit 5239653
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions fusio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod error;
#[cfg(feature = "fs")]
pub mod fs;
pub mod local;
pub mod parse;
pub mod path;
pub mod remotes;

Expand Down
87 changes: 87 additions & 0 deletions fusio/src/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::sync::Arc;

use crate::{DynFs, Error};

pub enum FsOptions {
Local,
#[cfg(feature = "aws")]
S3 {
bucket: String,
credential: Option<crate::remotes::aws::credential::AwsCredential>,
region: Option<String>,
sign_payload: Option<bool>,
checksum: Option<bool>,
},
}

impl FsOptions {
pub fn parse(self) -> Result<Arc<dyn DynFs>, Error> {
match self {
#[cfg(feature = "tokio")]
FsOptions::Local => Ok(Arc::new(crate::local::TokioFs)),
#[cfg(feature = "monoio")]
FsOptions::Local => Ok(Arc::new(crate::local::MonoIoFs)),
#[cfg(all(feature = "aws", feature = "object_store"))]
FsOptions::S3 {
bucket,
credential,
region,
sign_payload,
checksum,
} => {
use object_store::aws::AmazonS3Builder;

use crate::remotes::object_store::fs::S3Store;

let mut builder = AmazonS3Builder::new().with_bucket_name(bucket);

if let Some(credential) = credential {
builder = builder
.with_access_key_id(credential.key_id)
.with_secret_access_key(credential.secret_key);

if let Some(token) = credential.token {
builder = builder.with_token(token);
}
}
if let Some(region) = region {
builder = builder.with_region(region);
}
if let Some(sign_payload) = sign_payload {
builder = builder.with_unsigned_payload(!sign_payload);
}
if let Some(checksum) = checksum {
// TODO
}
Ok(Arc::new(S3Store::new(builder.build()?)))
}
#[cfg(feature = "aws")]
FsOptions::S3 {
bucket,
credential,
region,
sign_payload,
checksum,
} => {
use crate::remotes::aws::fs::AmazonS3Builder;

let mut builder = AmazonS3Builder::new(bucket);

if let Some(credential) = credential {
builder = builder.credential(credential);
}
if let Some(region) = region {
builder = builder.region(region);
}
if let Some(sign_payload) = sign_payload {
builder = builder.sign_payload(sign_payload);
}
if let Some(checksum) = checksum {
builder = builder.checksum(checksum);
}
Ok(Arc::new(builder.build()))
}
_ => Err(Error::Unsupported),
}
}
}
2 changes: 1 addition & 1 deletion fusio/src/remotes/aws/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod credential;
pub(crate) mod credential;
#[cfg(feature = "fs")]
pub mod fs;
pub(crate) mod options;
Expand Down
8 changes: 8 additions & 0 deletions fusio/src/remotes/object_store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ pub struct S3Store {
inner: Arc<AmazonS3>,
}

impl S3Store {
pub fn new(s3: AmazonS3) -> Self {
S3Store {
inner: Arc::new(s3),
}
}
}

impl Fs for S3Store {
type File = S3File;

Expand Down
2 changes: 1 addition & 1 deletion fusio/src/remotes/object_store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod fs;
pub mod fs;

use std::{ops::Range, sync::Arc};

Expand Down

0 comments on commit 5239653

Please sign in to comment.