How to read a big uploaded file line by line, using streams ? #2506
-
SummaryHi, I'm trying to parse large uploaded CSV files without having them in memory. However I'm a bit struggling and lost between all the streams in Rust, Tokio, and Axum. I have found From what I understood, I came up with the following example that counts the end of lines in the uploaded CSV chunk by chunk. async fn post_csv(body: axum::body::Body) -> Result<String, (StatusCode, String)> {
let mut count = 0usize;
let mut stream = body.into_data_stream();
loop {
let chunk = stream.try_next().await;
match chunk {
Ok(bytes) => match bytes {
Some(bytes) => count += bytes.into_iter().filter(|b| *b == b'\n').count(),
None => break,
},
Err(_) => {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
"Error reading body".to_string(),
))
}
}
}
Ok(count.to_string())
} I could continue from this snippet and implement some logic to identify the end of lines and re-chunks my chunks into chunks delimited by end of lines, before parsing the CSV chunks. But I feel like I'm re-implementing a bad wheel. It should be a simple solution using two magic lines and one or two axum version0.7 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Getting a stream of lines is a bit hard unfortunately but you can do it like this https://github.com/tokio-rs/axum/blob/main/axum-extra/src/json_lines.rs#L111 |
Beta Was this translation helpful? Give feedback.
Getting a stream of lines is a bit hard unfortunately but you can do it like this https://github.com/tokio-rs/axum/blob/main/axum-extra/src/json_lines.rs#L111