fusio_dispatch/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::sync::Arc;

use fusio::{DynFs, Error};

#[derive(Clone)]
#[non_exhaustive]
pub enum FsOptions {
    #[cfg(any(feature = "tokio", feature = "monoio"))]
    Local,
    #[cfg(feature = "aws")]
    S3 {
        bucket: String,
        credential: Option<fusio::remotes::aws::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(any(feature = "tokio", feature = "monoio"))]
            FsOptions::Local => Ok(Arc::new(fusio::disk::LocalFs {})),
            #[cfg(feature = "object_store")]
            FsOptions::S3 {
                bucket,
                credential,
                region,
                sign_payload,
                checksum,
            } => {
                use fusio_object_store::fs::S3Store;
                use object_store::aws::AmazonS3Builder;

                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 matches!(checksum, Some(true)) {
                    builder = builder.with_checksum_algorithm(object_store::aws::Checksum::SHA256);
                }
                Ok(Arc::new(S3Store::from(
                    builder.build().map_err(|e| fusio::Error::Other(e.into()))?,
                )) as Arc<dyn DynFs>)
            }
            #[cfg(all(feature = "aws", not(feature = "object_store")))]
            FsOptions::S3 {
                bucket,
                credential,
                region,
                sign_payload,
                checksum,
            } => {
                use fusio::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()))
            }
        }
    }
}