aws_multipart_upload/client/request/
complete.rs

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