【发布时间】:2020-06-11 10:25:32
【问题描述】:
我正在尝试构建一个从 SFTP 服务器提取文件并将它们上传到 S3 的服务。
对于 SFTP 部分,我使用的是async-ssh2,它为我提供了一个实现futures::AsyncRead 的文件处理程序。由于这些 SFTP 文件可能非常大,因此我试图将这个 File 处理程序转换为可以使用 Rusoto 上传的 ByteStream。看起来ByteStream 可以用futures::Stream 初始化。
我的计划是在File 对象上实现Stream(基于代码here)以与Rusoto 兼容(代码在下面复制以供后代使用):
use core::pin::Pin;
use core::task::{Context, Poll};
use futures::{ready, stream::Stream};
pub struct ByteStream<R>(R);
impl<R: tokio::io::AsyncRead + Unpin> Stream for ByteStream<R> {
type Item = u8;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let mut buf = [0; 1];
match ready!(Pin::new(&mut self.0).poll_read(cx, &mut buf)) {
Ok(n) if n != 0 => Some(buf[0]).into(),
_ => None.into(),
}
}
}
这是一个很好的方法吗?我看到了this question,但它似乎在使用tokio::io::AsyncRead。是否使用tokio 的规范方式来执行此操作?如果是这样,有没有办法从futures_io::AsyncRead 转换为tokio::io::AsyncRead?
【问题讨论】: