cdn-uploader/src/bin/upload.rs

73 lines
2.2 KiB
Rust

use clap::Command;
#[tokio::main]
async fn main() {
let bucket = clap::Arg::new("bucket")
.help("The bucket to upload to")
.long("bucket")
.short('b')
.takes_value(true)
.required(true);
let access_key = clap::Arg::new("access-key")
.help("The access key to use")
.long("access-key")
.short('a')
.takes_value(true)
.required(true);
let secret_key = clap::Arg::new("secret-key")
.help("The secret key to use")
.long("secret-key")
.short('s')
.takes_value(true)
.required(true);
let region = clap::Arg::new("region")
.help("The region to use")
.long("region")
.short('r')
.takes_value(true)
.default_value("us-east-1");
let endpoint = clap::Arg::new("endpoint")
.help("The endpoint to use")
.long("endpoint")
.short('e')
.takes_value(true);
let input = clap::Arg::new("input")
.help("The input directory")
.index(1)
.multiple_values(true);
let matches = Command::new("S3 Image Uploader")
.version("1.0")
.author("Julius")
.about("used to upload an image to s3")
.args(&[bucket, access_key, secret_key, region, endpoint, input])
.get_matches();
let u = cdn_upload::Uploader::new(
matches.value_of("bucket").unwrap(),
matches.value_of("access-key").unwrap(),
matches.value_of("secret-key").unwrap(),
matches.value_of("region").unwrap(),
matches.value_of("endpoint"),
)
.unwrap();
let r = u.existing_files().await.unwrap();
let m = matches.values_of("input").unwrap().map(ToString::to_string);
let handles = m.map(|arg| {
let u = u.clone();
let r = r.clone();
tokio::spawn(async move {
let filename = arg;
let data = cdn_upload::fix_image(filename.clone(), &r).unwrap();
for img in data {
u.upload(&img).await.unwrap();
println!("Uploaded {} as {}", filename, img.name());
}
})
})
.collect::<Vec<_>>();
for handle in handles {
handle.await.unwrap();
}
}