fusio_dispatch/
lib.rs

1use std::sync::Arc;
2
3use fusio::{DynFs, Error};
4
5#[derive(Clone)]
6#[non_exhaustive]
7pub enum FsOptions {
8    #[cfg(any(feature = "tokio", feature = "monoio", feature = "opfs"))]
9    Local,
10    #[cfg(feature = "aws")]
11    S3 {
12        bucket: String,
13        credential: Option<fusio::remotes::aws::AwsCredential>,
14        endpoint: Option<String>,
15        region: Option<String>,
16        sign_payload: Option<bool>,
17        checksum: Option<bool>,
18    },
19}
20
21impl FsOptions {
22    pub fn parse(self) -> Result<Arc<dyn DynFs>, Error> {
23        match self {
24            #[cfg(any(feature = "tokio", feature = "monoio", feature = "opfs"))]
25            FsOptions::Local => Ok(Arc::new(fusio::disk::LocalFs {})),
26            #[cfg(feature = "object_store")]
27            FsOptions::S3 {
28                bucket,
29                credential,
30                endpoint,
31                region,
32                sign_payload,
33                checksum,
34            } => {
35                use fusio_object_store::fs::S3Store;
36                use object_store::aws::AmazonS3Builder;
37
38                let mut builder = AmazonS3Builder::new().with_bucket_name(bucket);
39
40                if let Some(credential) = credential {
41                    builder = builder
42                        .with_access_key_id(credential.key_id)
43                        .with_secret_access_key(credential.secret_key);
44
45                    if let Some(token) = credential.token {
46                        builder = builder.with_token(token);
47                    }
48                }
49                if let Some(endpoint) = endpoint {
50                    builder = builder.with_endpoint(endpoint);
51                }
52                if let Some(region) = region {
53                    builder = builder.with_region(region);
54                }
55                if let Some(sign_payload) = sign_payload {
56                    builder = builder.with_unsigned_payload(!sign_payload);
57                }
58                if matches!(checksum, Some(true)) {
59                    builder = builder.with_checksum_algorithm(object_store::aws::Checksum::SHA256);
60                }
61                Ok(Arc::new(S3Store::from(
62                    builder.build().map_err(|e| fusio::Error::Other(e.into()))?,
63                )) as Arc<dyn DynFs>)
64            }
65            #[cfg(all(feature = "aws", not(feature = "object_store")))]
66            FsOptions::S3 {
67                bucket,
68                credential,
69                endpoint,
70                region,
71                sign_payload,
72                checksum,
73            } => {
74                use fusio::remotes::aws::fs::AmazonS3Builder;
75
76                let mut builder = AmazonS3Builder::new(bucket);
77
78                if let Some(credential) = credential {
79                    builder = builder.credential(credential);
80                }
81                if let Some(endpoint) = endpoint {
82                    builder = builder.endpoint(endpoint);
83                }
84                if let Some(region) = region {
85                    builder = builder.region(region);
86                }
87                if let Some(sign_payload) = sign_payload {
88                    builder = builder.sign_payload(sign_payload);
89                }
90                if let Some(checksum) = checksum {
91                    builder = builder.checksum(checksum);
92                }
93                Ok(Arc::new(builder.build()))
94            }
95        }
96    }
97}