Streamer is a handy tool deal with stream, it provides a set of structs and trait to stream
FileString&'static strVec<T>[T; N]Box<[T]>
Some example to walk you through:
echo "1234567890abcdefghijklmnopqrstuvw" >> infouse streamer::{Stream, StreamExt, Streaming};
async fn run() {
let file = File::open("info").unwrap();
let streaming = Streaming::from(file);
streaming
.chunks(5)
.take(3)
.for_each(|en| async move {
println!("stream 5 bytes as a chunk: {:?}", std::str::from_utf8(&en).unwrap());
})
.await;
}it output:
stream 5 bytes as a chunk: 12345
stream 5 bytes as a chunk: 67890
stream 5 bytes as a chunk: abcdeasync fn run() {
// let s = "1234567890abcdefghijklmnopqrstuvw"; // output the same as above
// let s = "1234567890abcdefghijklmnopqrstuvw".to_string(); // output the same as above
let s: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let s = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let streaming = Streaming::from(s);
streaming
.chunks(4)
.take(3)
.for_each(|en| async move {
println!("{:?}", &en);
})
.await;
}it output:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 0]Since I am heavy user of hyper,
send body of request is not that convenient as others
especially for streaming image, video document and so on.
it enable you stream large file in chunks in HTTP Format
To use, enable hyper feature
[dependencies]
streamer = { version = "*", features = ["hyper"] }use hyper::{Body, Request}:
let file = File::open("info").unwrap();
let mut streaming = Streamer::new(file);
streaming.meta.set_buf_len(10); // length sent as a chunk, the default is 64kB
streaming.meta.set_name("doc"); // field name
streaming.meta.set_filename("info"); // file name
let body: Body = streaming.streaming();
// build a request
let request: Request<Body> = Request::post("<uri-here>").body(body).expect("failed to build a request");it will sennd the file info in 10-bytes chunks, each chunk are formated into HTTP multipart/form-data, with this you can split large file and stream it chunks by chunks.
May be some compression could be done to lower network traffic
- Streaming Chunks Compression