Upload and download with Axum, streaming #1638
Unanswered
frederikhors
asked this question in
Q&A
Replies: 1 comment 9 replies
-
// `field` is of type `axum::extract::multipart::Field` which implements `Stream` as seen here
// https://docs.rs/axum/latest/axum/extract/multipart/struct.Field.html#impl-Stream-for-Field%3C%27a%3E
// `map_err` comes from https://docs.rs/futures/latest/futures/future/trait.TryFutureExt.html#method.map_err which
// produces a new stream with a different error type, similarly to `Result::map_err`
let body_with_io_error = field.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
// `StreamReader` comes from
// https://docs.rs/tokio-util/latest/tokio_util/io/struct.StreamReader.html and like the docs says
// it "Convert a Stream of byte chunks into an AsyncRead." so `body_reader` is something that
// implements `AsyncRead`
let body_reader = StreamReader::new(body_with_io_error);
// This pins value on the stack but it doesn't appear to be necessary
futures::pin_mut!(body_reader); This works as well let mut body_reader = StreamReader::new(body_with_io_error);
bucket
.put_object_stream(&mut body_reader, filename)
.await
.unwrap();
|
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to write a small axum router that uploads and downloads files.
I would like to stream those files directly to the cloud, but I don't know what to use for streaming.
In Go (my primarily language) I'm using
http.ResponseWriter
withio.Copy
.But in Rust I'm having real difficulties to understand what to use, this is the code:
QUESTIONS
The
upload
function works but I don't know what these lines mean:The
download
function doesn't work because I don't know what to use, how to create thewriter
thatget_object_stream()
needs.I can PR examples and docs, as soon as I figure out how to do it, with your help.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions