Turning a file into futures Stream

I want to turn a file (tokio::fs::File or std::fs::File) into a futures::stream::Steam of bytes, so that i can construct this ByteStream from it, so that rusoto can upload while reading from file. Is this possible at all?

You can use tokio::codec::FramedRead for this. playground

use bytes::BytesMut;
use futures::{Future, Stream};
use tokio::codec::{BytesCodec, FramedRead};
use tokio::fs::File;
use tokio::runtime::Runtime;

fn main() {
    let mut rt = Runtime::new().unwrap();
    let task = File::open("Cargo.toml").and_then(|file| {
        FramedRead::new(file, BytesCodec::new())
            .map(BytesMut::freeze) // Map stream of `BytesMut` to stream of `Bytes`
            .concat2()
            .and_then(|bytes| {
                assert_eq!(bytes, &include_bytes!("../Cargo.toml")[..]);
                Ok(())
            })
    });

    if let Err(e) = rt.block_on(task) {
        dbg!(e);
    }
}
1 Like

Thanks!