Skip to content

Commit 0e3d743

Browse files
committed
Split upload map into separate function
This makes things cleaner than a big `async move` block Signed-off-by: Robert Detjens <[email protected]>
1 parent d039cf6 commit 0e3d743

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/deploy/s3.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::fs::File;
2-
use std::path::PathBuf;
2+
use std::path::{Path, PathBuf};
33

44
use anyhow::{anyhow, bail, Context, Error, Ok, Result};
55
use futures::future::try_join_all;
66
use itertools::Itertools;
7+
use s3::Bucket;
78
use simplelog::*;
89
use tokio;
910

@@ -37,20 +38,10 @@ pub async fn upload_assets(
3738
let uploaded = result
3839
.assets
3940
.iter()
40-
.map(|asset| async move {
41-
let path_in_bucket = format!(
42-
"assets/{chal_slug}/{file}",
43-
chal_slug = chal.directory.to_string_lossy(),
44-
file = asset.file_name().unwrap().to_string_lossy()
45-
);
46-
47-
trace!("uploading {:?} to bucket path {:?}", asset, &path_in_bucket);
48-
49-
// TODO: move to async/streaming to better handle large files and report progress
50-
let mut asset_file = tokio::fs::File::open(asset).await?;
51-
bucket.put_object_stream_blocking(&mut asset_file, &path_in_bucket)?;
52-
53-
Ok(path_in_bucket.into())
41+
.map(|asset_file| async move {
42+
upload_single_file(bucket, chal, asset_file)
43+
.await
44+
.with_context(|| format!("failed to upload file {asset_file:?}"))
5445
})
5546
.try_join_all()
5647
.await
@@ -67,3 +58,27 @@ pub async fn upload_assets(
6758
.try_join_all()
6859
.await
6960
}
61+
62+
async fn upload_single_file(
63+
bucket: &Bucket,
64+
chal: &ChallengeConfig,
65+
file: &Path,
66+
) -> Result<PathBuf> {
67+
// e.g. s3.example.domain/assets/misc/foo/stuff.zip
68+
let path_in_bucket = format!(
69+
"assets/{chal_slug}/{file}",
70+
chal_slug = chal.directory.to_string_lossy(),
71+
file = file.file_name().unwrap().to_string_lossy()
72+
);
73+
74+
trace!("uploading {:?} to bucket path {:?}", file, &path_in_bucket);
75+
76+
// TODO: move to async/streaming to better handle large files and report progress
77+
let mut asset_file = tokio::fs::File::open(file).await?;
78+
let r = bucket
79+
.put_object_stream(&mut asset_file, &path_in_bucket)
80+
.await?;
81+
trace!("uploaded {} bytes for file {:?}", r.uploaded_bytes(), file);
82+
83+
Ok(PathBuf::from(path_in_bucket))
84+
}

0 commit comments

Comments
 (0)