Skip to main content

aws_multipart_upload/client/request/
complete.rs

1use std::fmt::{self, Debug, Formatter};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use aws_sdk::types::CompletedMultipartUpload;
6use futures::future::BoxFuture;
7
8use super::CompleteRequestBuilder;
9use crate::client::part::{CompletedParts, EntityTag};
10use crate::client::{UploadClient, UploadData, UploadId};
11use crate::error::{ErrorRepr, Result};
12use crate::uri::ObjectUri;
13
14/// Sending a request to complete an upload.
15pub struct SendCompleteUpload(
16    pub(crate) BoxFuture<'static, Result<CompletedUpload>>,
17);
18
19impl SendCompleteUpload {
20    /// Create a new `SendCompleteUpload`.
21    pub fn new(client: &UploadClient, req: CompleteRequest) -> Self {
22        let cli = client.clone();
23        Self(Box::pin(async move { cli.inner.send_complete_upload(req).await }))
24    }
25}
26
27impl Future for SendCompleteUpload {
28    type Output = Result<CompletedUpload>;
29
30    fn poll(
31        mut self: Pin<&mut Self>,
32        cx: &mut Context<'_>,
33    ) -> Poll<Self::Output> {
34        self.0.as_mut().poll(cx)
35    }
36}
37
38impl Debug for SendCompleteUpload {
39    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40        f.debug_tuple("SendCompleteUpload").finish()
41    }
42}
43
44/// Request object for completing a multipart upload.
45#[derive(Debug, Clone)]
46pub struct CompleteRequest {
47    pub(crate) id: UploadId,
48    pub(crate) uri: ObjectUri,
49    pub(crate) completed: CompletedParts,
50}
51
52impl CompleteRequest {
53    /// Create a new `CompleteRequest` from the upload data.
54    pub fn new(data: &UploadData, completed: CompletedParts) -> Self {
55        Self { id: data.get_id(), uri: data.get_uri(), completed }
56    }
57
58    /// Set the required properties on the SDK request builder for the
59    /// operation.
60    pub fn with_builder(
61        &self,
62        builder: CompleteRequestBuilder,
63    ) -> CompleteRequestBuilder {
64        let parts = CompletedMultipartUpload::from(&self.completed);
65        builder
66            .upload_id(&*self.id)
67            .bucket(&*self.uri.bucket)
68            .key(&*self.uri.key)
69            .multipart_upload(parts)
70    }
71
72    /// Returns a reference to the assigned `UploadId` for this request.
73    pub fn id(&self) -> &UploadId {
74        &self.id
75    }
76
77    /// Returns a reference to the `ObjectUri` for this request.
78    pub fn uri(&self) -> &ObjectUri {
79        &self.uri
80    }
81
82    /// Returns a reference to the `CompletedParts` for this request.
83    pub fn completed_parts(&self) -> &CompletedParts {
84        &self.completed
85    }
86
87    pub(crate) fn validate(&self) -> Result<()> {
88        if self.id.is_empty()
89            || self.uri.is_empty()
90            || self.completed_parts().count() == 0
91        {
92            return Err(ErrorRepr::Missing(
93                "CompleteUploadRequest",
94                "invalid upload data",
95            )
96            .into());
97        }
98        Ok(())
99    }
100}
101
102/// The value for a successful multipart upload.
103#[derive(Debug, Clone, Default)]
104pub struct CompletedUpload {
105    /// The URI of the created object.
106    pub uri: ObjectUri,
107    /// The entity tag of the created object.
108    pub etag: EntityTag,
109}
110
111impl CompletedUpload {
112    /// Create a new value from object URI and entity tag.
113    pub fn new(uri: ObjectUri, etag: EntityTag) -> Self {
114        Self { uri, etag }
115    }
116}