aws_multipart_upload/
client.rs

1pub mod aws;
2pub mod fs;
3pub mod hashmap;
4
5use aws_sdk_s3::primitives::ByteStream;
6use futures::future::BoxFuture;
7
8use crate::{
9    types::{api::*, UploadClient},
10    AwsError,
11};
12
13/// The concrete implementations of `UploadClient` exposed by this crate do not
14/// implement `on_upload_complete`.  To add a custom callback to be called after
15/// an upload was completed, you can implement this trait to add to an existing
16/// client and use with the extension trait below.
17pub trait OnComplete<U>
18where
19    U: UploadClient,
20{
21    fn on_upload_complete<'a, 'c: 'a>(
22        &'c self,
23        client: &'a U,
24        etag: EntityTag,
25    ) -> BoxFuture<'a, Result<(), AwsError>>;
26}
27
28/// A wrapper of an upload client that implements the one provided method.
29pub struct WithCallback<U, F> {
30    inner: U,
31    callback: F,
32}
33
34impl<U, F> WithCallback<U, F> {
35    pub fn new(inner: U, callback: F) -> Self {
36        Self { inner, callback }
37    }
38}
39
40impl<U, F> UploadClient for WithCallback<U, F>
41where
42    U: UploadClient + Send + Sync,
43    F: OnComplete<U> + Send + Sync,
44{
45    fn new_upload<'a, 'client: 'a>(
46        &'client self,
47        addr: &'a UploadAddress,
48    ) -> BoxFuture<'a, Result<UploadRequestParams, AwsError>> {
49        self.inner.new_upload(addr)
50    }
51
52    fn upload_part<'a, 'client: 'a>(
53        &'client self,
54        params: &'a UploadRequestParams,
55        part_number: i32,
56        part: ByteStream,
57    ) -> BoxFuture<'a, Result<EntityTag, AwsError>> {
58        self.inner.upload_part(params, part_number, part)
59    }
60
61    fn complete_upload<'a, 'client: 'a>(
62        &'client self,
63        params: &'a UploadRequestParams,
64        parts: &'a UploadedParts,
65    ) -> BoxFuture<'a, Result<EntityTag, AwsError>> {
66        self.inner.complete_upload(params, parts)
67    }
68
69    fn on_upload_complete(&self, etag: EntityTag) -> BoxFuture<'_, Result<(), AwsError>> {
70        Box::pin(async move {
71            self.callback.on_upload_complete(&self.inner, etag).await?;
72            Ok(())
73        })
74    }
75}
76
77impl<U> UploadClientExt for U where U: UploadClient {}
78
79/// An extension trait adding convenience methods to existing `UploadClient`s.
80pub trait UploadClientExt
81where
82    Self: UploadClient,
83{
84    /// Adds a custom implementation of the default method `on_upload_complete`
85    /// adding specific handling of the entity tag of the completed upload in
86    /// case it is required for some functionality.
87    fn with_callback<F>(self, callback: F) -> WithCallback<Self, F>
88    where
89        Self: Sized,
90        F: OnComplete<Self>,
91    {
92        WithCallback::new(self, callback)
93    }
94}