use std::process::Stdio;
use super::aws_s3_command;
use crate::common::*;
use crate::tokio_glue::copy_stream_to_writer;
#[instrument(level = "trace", skip(data))]
pub(crate) async fn upload_file<'a>(
data: BoxStream<BytesMut>,
file_url: &'a Url,
) -> Result<()> {
debug!("uploading stream to `aws s3`");
let mut child = aws_s3_command()
.await?
.args(["cp", "-", file_url.as_str()])
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.context("error running `aws s3`")?;
let child_stdin = child.stdin.take().expect("child should have stdin");
copy_stream_to_writer(data, child_stdin)
.await
.context("error copying data to `aws s3`")?;
let status = child
.wait()
.await
.with_context(|| format!("error finishing upload to {}", file_url))?;
if status.success() {
Ok(())
} else {
Err(format_err!("`aws s3` returned error: {}", status))
}
}