s3handler 0.9.0

An s3 handler for s3rs nu-shell-s3-plugin
Documentation
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use super::file::FilePool;
use crate::error::Error;
use crate::tokio_async::traits::{DataPool, Filter, S3Folder};
use crate::utils::S3Object;
use url::Url;

#[derive(Debug)]
pub enum PoolType {
    UpPool,
    DownPool,
}

#[derive(Debug)]
pub struct Canal {
    pub up_pool: Option<Box<dyn DataPool>>,
    pub upstream_object: Option<S3Object>,
    pub down_pool: Option<Box<dyn DataPool>>,
    pub downstream_object: Option<S3Object>,
    pub(crate) default: PoolType,
    pub filter: Option<Filter>,
    // TODO: feature: data transformer
    // it may do encrypt, or format transformation here
    // upstream_obj_lambda:
    // downstream_obj_lambda:

    // TODO: folder/bucket upload feature:
    // index & key of S3Object transformer
    // upstream_obj_desc_lambda:
    // downstream_obj_desc_lambda:
}

/// A canal presets a object link for two object from resource pool to pool.
/// If everything is set, the async api can pull/push the objects.
///
/// The `download_file`, and `upload_file` api will setup the up pool as s3 pool,
/// and set up the down pool as file pool for the most usage case.
///
/// The terms `object`, `key`, `bucket`, `folder`, `path` may be easiler for readness in coding,
/// so there are several similar methods help you to setup things.
/// If you down want these duplicate functions, you can enable the `slim` feature.
impl Canal {
    /// Check the two pools are set or not
    pub fn is_connect(&self) -> bool {
        self.up_pool.is_some() && self.down_pool.is_some()
    }

    /// Set downd pool as file pool, and toward to the `resource_location`
    pub fn toward(mut self, resource_location: &str) -> Result<Self, Error> {
        self.toward_pool(Box::new(FilePool::new(resource_location)?));
        self.upstream_object = Some(resource_location.into());
        Ok(self)
    }

    /// Set up pool as file pool, and from to the `resource_location`
    pub fn from(mut self, resource_location: &str) -> Result<Self, Error> {
        self.from_pool(Box::new(FilePool::new(resource_location)?));
        self.downstream_object = Some(resource_location.into());
        Ok(self)
    }

    /// Download object from s3 pool to file pool
    /// This function set file pool as down pool and s3 pool as up pool
    /// then toward to the `resource_location`,
    /// pull the object from uppool into down pool.
    pub async fn download_file(mut self, resource_location: &str) -> Result<(), Error> {
        if let Ok(r) = Url::parse(resource_location) {
            self.toward_pool(Box::new(FilePool::new(&r.scheme())?)); // for C://
        } else {
            self.toward_pool(Box::new(FilePool::new("/")?));
        }
        self.downstream_object = Some(resource_location.into());
        match self.downstream_object.take() {
            Some(S3Object { bucket, key, .. }) if key.is_none() => {
                self.downstream_object = Some(S3Object {
                    bucket,
                    key: self.upstream_object.clone().unwrap().key,
                    ..Default::default()
                });
            }
            Some(obj) => {
                self.downstream_object = Some(obj);
            }
            None => {
                panic!("never be here")
            }
        }
        Ok(self.pull().await?)
    }

    /// Upload object from file pool to s3 pool
    /// This function set file pool as down pool and s3 pool as up pool
    /// then toward to the `resource_location`,
    /// push the object from uppool into down pool.
    pub async fn upload_file(mut self, resource_location: &str) -> Result<(), Error> {
        if let Ok(r) = Url::parse(resource_location) {
            self.toward_pool(Box::new(FilePool::new(&r.scheme())?)); // for C://
        } else {
            self.toward_pool(Box::new(FilePool::new("/")?));
        }
        self.downstream_object = Some(resource_location.into());
        match self.downstream_object.take() {
            Some(S3Object { bucket, key, .. }) if key.is_none() => {
                self.downstream_object = Some(S3Object {
                    bucket: Some(std::env::current_dir()?.to_string_lossy()[1..].into()),
                    key: Some(format!("/{}", bucket.unwrap_or_default())),
                    ..Default::default()
                });
            }
            Some(obj) => {
                self.downstream_object = Some(obj);
            }
            None => {
                panic!("never be here")
            }
        }
        Ok(self.push().await?)
    }
    // End of short cut api to file pool

    // Begin of setting api
    /// Setup the up pool
    pub fn from_pool(&mut self, pool: Box<dyn DataPool>) {
        self.up_pool = Some(pool);
    }

    /// Setup the down pool
    pub fn toward_pool(&mut self, pool: Box<dyn DataPool>) {
        self.down_pool = Some(pool);
    }

    #[inline]
    pub fn _object(mut self, object_name: &str) -> Self {
        let mut o = match self.default {
            PoolType::UpPool => self.upstream_object.take(),
            PoolType::DownPool => self.downstream_object.take(),
        }
        .unwrap_or_default();
        o.key = if object_name.starts_with('/') {
            Some(object_name.to_string())
        } else {
            Some(format!("/{}", object_name))
        };
        match self.default {
            PoolType::UpPool => self.upstream_object = Some(o),
            PoolType::DownPool => self.downstream_object = Some(o),
        };
        self
    }

    #[inline]
    pub fn _bucket(mut self, bucket_name: &str) -> Self {
        let mut o = match self.default {
            PoolType::UpPool => self.upstream_object.take(),
            PoolType::DownPool => self.downstream_object.take(),
        }
        .unwrap_or_default();
        o.bucket = Some(bucket_name.to_string());
        match self.default {
            PoolType::UpPool => self.upstream_object = Some(o),
            PoolType::DownPool => self.downstream_object = Some(o),
        };
        self
    }

    /// Setup the object for the first pool connected by canal,
    /// This api can be used without fully setting up two pools,
    /// and just set up the object as you what you think.
    pub fn object(self, object_name: &str) -> Self {
        self._object(object_name)
    }

    /// The same as `object()`
    #[cfg(not(feature = "slim"))]
    pub fn key(self, key_name: &str) -> Self {
        self._object(key_name)
    }

    /// Setup the bucket for the first pool connected by canal,
    /// This api can be used without fully setting up two pools,
    /// and just set up the object as you what you think.
    pub fn bucket(self, bucket_name: &str) -> Self {
        self._bucket(bucket_name)
    }

    #[cfg(not(feature = "slim"))]
    /// The same as `bucket()`
    pub fn folder(self, folder_name: &str) -> Self {
        self._bucket(folder_name)
    }

    pub fn prefix(mut self, prefix_str: &str) -> Self {
        self.filter = Some(Filter::Prefix(prefix_str.into()));
        self
    }

    #[inline]
    pub fn _toward_object(&mut self, object_name: &str) {
        let mut o = self.downstream_object.take().unwrap_or_default();
        o.key = if object_name.starts_with('/') {
            Some(object_name.to_string())
        } else {
            Some(format!("/{}", object_name))
        };
        self.downstream_object = Some(o);
    }

    /// Setup the object in the down pool
    pub fn toward_object(&mut self, object_name: &str) {
        self._toward_object(object_name)
    }

    /// The same as `toward_object()`
    #[cfg(not(feature = "slim"))]
    pub fn toward_key(&mut self, object_name: &str) {
        self._toward_object(object_name)
    }

    #[inline]
    pub fn _toward_bucket(&mut self, bucket_name: &str) {
        let mut o = self.downstream_object.take().unwrap_or_default();
        o.bucket = Some(bucket_name.to_string());
        self.downstream_object = Some(o);
    }

    /// Setup the bucket in the down pool
    pub fn toward_bucket(&mut self, bucket_name: &str) {
        self._toward_bucket(bucket_name)
    }

    /// The same as `toward_bucket()`
    #[cfg(not(feature = "slim"))]
    pub fn toward_folder(&mut self, folder_name: &str) {
        self._toward_bucket(folder_name)
    }

    /// Setup the path in the down pool
    #[cfg(not(feature = "slim"))]
    pub fn toward_path(&mut self, path: &str) {
        self.downstream_object = Some(path.into());
    }

    #[inline]
    pub fn _from_object(&mut self, object_name: &str) {
        let mut o = self.upstream_object.take().unwrap_or_default();
        o.key = if object_name.starts_with('/') {
            Some(object_name.to_string())
        } else {
            Some(format!("/{}", object_name))
        };
        self.upstream_object = Some(o);
    }

    /// Setup the object in the up pool
    pub fn from_object(&mut self, object_name: &str) {
        self._from_object(object_name)
    }

    /// The same as `from_object()`
    #[cfg(not(feature = "slim"))]
    pub fn from_key(&mut self, object_name: &str) {
        self._from_object(object_name)
    }

    #[inline]
    pub fn _from_bucket(&mut self, bucket_name: &str) {
        let mut o = self.upstream_object.take().unwrap_or_default();
        o.bucket = Some(bucket_name.to_string());
        self.upstream_object = Some(o);
    }

    /// Setup the bucket in the up pool
    pub fn from_bucket(&mut self, bucket_name: &str) {
        self._from_bucket(bucket_name)
    }

    /// The same as `from_bucket()`
    #[cfg(not(feature = "slim"))]
    pub fn from_folder(&mut self, folder_name: &str) {
        self._from_bucket(folder_name)
    }

    /// Setup the path in the up pool
    #[cfg(not(feature = "slim"))]
    pub fn from_path(&mut self, path: &str) {
        self.upstream_object = Some(path.into());
    }
    // End of setting api

    // Begin of IO api
    /// Push the object from down pool to up pool.
    pub async fn push(self) -> Result<(), Error> {
        match (self.up_pool, self.down_pool) {
            (Some(up_pool), Some(down_pool)) => {
                if let Some(downstream_object) = self.downstream_object {
                    let b = down_pool.pull(downstream_object.clone()).await?;
                    up_pool
                        .push(self.upstream_object.unwrap_or(downstream_object), b)
                        .await?;
                    Ok(())
                } else {
                    Err(Error::NoObject())
                }
            }
            _ => Err(Error::PoolUninitializeError()),
        }
    }

    /// Push a specified object from up pool to down pool
    pub async fn push_obj(&self, obj: S3Object) -> Result<(), Error> {
        match (&self.up_pool, &self.down_pool) {
            (Some(up_pool), Some(down_pool)) => {
                let b = down_pool.pull(obj.clone()).await?;
                up_pool.push(obj, b).await?;
                Ok(())
            }
            _ => Err(Error::PoolUninitializeError()),
        }
    }

    /// Pull the object from up pool to down pool.
    pub async fn pull(self) -> Result<(), Error> {
        match (self.up_pool, self.down_pool) {
            (Some(up_pool), Some(down_pool)) => {
                if let Some(upstream_object) = self.upstream_object {
                    let b = up_pool.pull(upstream_object.clone()).await?;
                    down_pool
                        .push(self.downstream_object.unwrap_or(upstream_object), b)
                        .await?;
                    Ok(())
                } else {
                    Err(Error::NoObject())
                }
            }
            _ => Err(Error::PoolUninitializeError()),
        }
    }

    /// Pull a specified object from up pool to down pool
    pub async fn pull_obj(&self, obj: S3Object) -> Result<(), Error> {
        match (&self.up_pool, &self.down_pool) {
            (Some(up_pool), Some(down_pool)) => {
                let b = up_pool.pull(obj.clone()).await?;
                down_pool.push(obj, b).await?;
                Ok(())
            }
            _ => Err(Error::PoolUninitializeError()),
        }
    }

    /// Remove the object in the up pool.
    pub async fn upstream_remove(self) -> Result<(), Error> {
        if let Some(upstream_object) = self.upstream_object {
            Ok(self
                .up_pool
                .expect("upstream pool should exist") // TODO customize Error
                .remove(upstream_object)
                .await?)
        } else {
            Err(Error::ResourceUrlError(
                "can not remove on an object withouput setup".to_string(),
            ))
        }
    }

    /// Remove the object in the down pool.
    pub async fn downstream_remove(self) -> Result<(), Error> {
        if let Some(downstream_object) = self.downstream_object {
            Ok(self
                .down_pool
                .expect("downstream pool should exist") // TODO customize Error
                .remove(downstream_object)
                .await?)
        } else {
            Err(Error::ResourceUrlError(
                "can not remove on an object withouput setup".to_string(),
            ))
        }
    }

    /// Remove the object depence on the first pool connected by the canal
    /// This api can be used without fully setting up two pools,
    /// and remove object as you what you think.
    pub async fn remove(self) -> Result<(), Error> {
        match self.default {
            PoolType::UpPool => self.upstream_remove().await,
            PoolType::DownPool => self.downstream_remove().await,
        }
    }

    /// List the objects in the up pool.
    pub async fn upstream_list(self) -> Result<Box<dyn S3Folder>, Error> {
        Ok(self
            .up_pool
            .expect("upstream pool should exist")
            .list(self.upstream_object, &self.filter)
            .await?)
    }

    /// List the objects in the down pool.
    pub async fn downstream_list(self) -> Result<Box<dyn S3Folder>, Error> {
        Ok(self
            .down_pool
            .expect("downstream pool should exist")
            .list(self.downstream_object, &self.filter)
            .await?)
    }

    /// List the objects depence on the first pool connected by the canal
    /// This api can be used without fully setting up two pools,
    /// and list objects as you what you think.
    pub async fn list(self) -> Result<Box<dyn S3Folder>, Error> {
        match self.default {
            PoolType::UpPool => self.upstream_list().await,
            PoolType::DownPool => self.downstream_list().await,
        }
    }

    // pub async fn sync(self)
    // End of IO api
}