Optimal way to proxy large files through axum using reqwest? #1821
-
I'm trying to proxy large files through What I have already tried: async fn proxy_req(/* parameters */) -> impl IntoResponse {
let reqwest_response = client.get(&uri).send.await.unwrap();
// Here I can get bytes from the response and return that
let data = reqwest_response.bytes().await.unwrap();
data
// But how do I convert `reqwest_response` into a stream of data that
// doesn't blow through all of my RAM if the response is huge, and is done
// in a streaming / async fashion?
} I am open to trying other HTTP clients if that helps. Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Call |
Beta Was this translation helpful? Give feedback.
-
Thanks @davidpdrsn, your answer pointed me in the right direction! Using Response::bytes_stream and axum::body::StreamBody seems to do the right thing. async fn proxy_req(/* parameters */) -> impl IntoResponse {
let reqwest_response = client.get(&uri).send.await.unwrap();
let stream = reqwest_response.bytes_stream();
StreamBody::new(stream)
} |
Beta Was this translation helpful? Give feedback.
Call
Response::bytes_stream
and do something like this https://github.com/tokio-rs/axum/blob/main/examples/stream-to-file/src/main.rs#L104