超过Rust Axum多部件长度限制



参考Axum文档:docs.rs


大家好,我正在尝试使用HTML5表单和Rust Axum创建一个简单的文件上传。

问题是,虽然任何正常的文件工作,较大的文件(特别是视频文件),我想上传太大。Axum(后来的Tokio)恐慌,因为Field大小对于文件上传来说太大了。

我似乎找不到任何关于扩展流限制的有用信息。

<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" accept="video/mp4">
<input type="submit" value="Upload video">
</form>
async fn upload(mut multipart: Multipart) {
while let Some(mut field) = multipart.next_field().await.unwrap() {
let name = field.name().unwrap().to_string();
let data = field.bytes().await.unwrap();
println!("Length of `{}` is {} bytes", name, data.len());
}
}
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: MultipartError { source: failed to read stream: failed to read stream: length limit exceeded }', src/main.rs:84:40
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: MultipartError { source: failed to read stream: failed to read stream: length limit exceeded }', src/main.rs:84:40
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: MultipartError { source: failed to read stream: failed to read stream: length limit exceeded }', src/main.rs:84:40

为了不影响这段代码的主要部分,我省略了路由器,但它的应用程序是:.route("/upload", post(upload)),根据Axum文档中显示的示例。

快速提示:要启用Multipart文件上传,必须添加Cargo特性标志" Multipart "阿克苏姆。

任何帮助都是感激的。谢谢你。

您尝试过Axum的DefaultBodyLimit服务吗?

use axum::{
Router,
routing::post,
body::Body,
extract::{DefaultBodyLimit, RawBody},
http::Request,
};
let app = Router::new()
.route(
"/",
// even with `DefaultBodyLimit` the request body is still just `Body`
post(|request: Request<Body>| async {}),
)
.layer(DefaultBodyLimit::max(1024));

我在这里找到了这个例子

最新更新