我需要从 OpenDAL 读取器构建一个 Axum Body,但找不到任何方法。有人知道怎么做吗?示例代码:
use axum::body::Body;
use opendal::{services, Operator};
use http_body_util::StreamBody;
async fn to_body() -> Body {
let op = Operator::new(services::Fs::default().root("./")).unwrap().finish();
let reader = op.reader("../Cargo.toml").await.unwrap();
let stream = reader.into_stream(..).await.unwrap();
// stream implements futures_core::stream::Stream so I expected this to work, but it doesn't:
// let body = Body::from_stream(stream);
// I can build a StreamBody but then I fail to create a Body from it
let stream_body = StreamBody::new(stream).into();
// Now I am not able to convert the stream_body to a Body
}
改用
.into_bytes_stream()
:然后,可以将结果流直接传递给
Body::from_stream
:之所以有效,是因为
Stream<Item = Result<Bytes, _>>
这是一种半标准化的字节流表示方式。唯一的潜在问题可能是错误转换,但 OpenDAL 流使用的std::io::Error
错误类型已经可以直接转换为Box<dyn Error>
axum 使用的错误类型。