Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit a9dfc28

Browse files
committed
abort multipart form in the presence of errors
1 parent 270f5e5 commit a9dfc28

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

bottomless/src/read.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub async fn upload_s3_multipart(
6363
client: &aws_sdk_s3::Client,
6464
key: &str,
6565
bucket: &str,
66-
mut reader: impl AsyncRead + Unpin,
66+
reader: impl AsyncRead + Send + Unpin,
6767
) -> Result<()> {
6868
let upload_id = client
6969
.create_multipart_upload()
@@ -74,6 +74,46 @@ pub async fn upload_s3_multipart(
7474
.upload_id
7575
.ok_or_else(|| anyhow::anyhow!("missing upload_id"))?;
7676

77+
let parts = upload_s3_parts(client, upload_id.as_str(), key, bucket, reader).await;
78+
79+
match parts {
80+
Ok(parts) => {
81+
client
82+
.complete_multipart_upload()
83+
.upload_id(upload_id)
84+
.bucket(bucket)
85+
.key(key)
86+
.multipart_upload(
87+
CompletedMultipartUpload::builder()
88+
.set_parts(Some(parts))
89+
.build(),
90+
)
91+
.send()
92+
.await?;
93+
94+
Ok(())
95+
}
96+
Err(err) => {
97+
client
98+
.abort_multipart_upload()
99+
.upload_id(upload_id)
100+
.bucket(bucket)
101+
.key(key)
102+
.send()
103+
.await?;
104+
105+
Err(err)
106+
}
107+
}
108+
}
109+
110+
async fn upload_s3_parts(
111+
client: &aws_sdk_s3::Client,
112+
upload_id: &str,
113+
key: &str,
114+
bucket: &str,
115+
mut reader: impl AsyncRead + Unpin,
116+
) -> Result<Vec<CompletedPart>> {
77117
let chunk_sizes = &[
78118
5 * 1024 * 1024,
79119
10 * 1024 * 1024,
@@ -186,18 +226,5 @@ pub async fn upload_s3_multipart(
186226
);
187227
}
188228

189-
client
190-
.complete_multipart_upload()
191-
.upload_id(upload_id)
192-
.bucket(bucket)
193-
.key(key)
194-
.multipart_upload(
195-
CompletedMultipartUpload::builder()
196-
.set_parts(Some(parts))
197-
.build(),
198-
)
199-
.send()
200-
.await?;
201-
202-
Ok(())
229+
Ok(parts)
203230
}

0 commit comments

Comments
 (0)