使用 hyper 将块流异步写入文件
Writing a chunk stream to a file asynchronously using hyper
我正在尝试创建一个使用 hyper 将远程文件下载到本地文件路径的简单函数。我也需要异步写入文件(在我的例子中,我使用 tokio_fs
)。这是代码:
// Parts of the code were omitted, see the playground for full source code
pub fn download_file(
uri: Uri,
file_location: &Path,
) -> Box<Future<Item = (), Error = DownloadFileError>> {
let temp_dir_path = tempfile::tempdir().unwrap().into_path();
let file_name = match file_location.file_name() {
Some(file_name) => file_name,
None => return Box::new(futures::failed(DownloadFileError::IncorrectFilePath)),
};
let temp_filepath = temp_dir_path.join(&file_name);
let connector = HttpsConnector::new(2).unwrap();
let client: Client<_, Body> = Client::builder().build(connector);
let response_future = client
.get(uri)
.map_err(|err| DownloadFileError::GetRequest(err));
let create_file_future =
File::create(temp_filepath).map_err(|err| DownloadFileError::CreateFile(err));
Box::new(
response_future
.join(create_file_future)
.and_then(move |(res, file)| {
res.into_body()
.map_err(|e| DownloadFileError::GetRequest(e))
.for_each(move |chunk| {
io::write_all(file, chunk)
.map(|_| ())
.map_err(|_| DownloadFileError::FileWrite)
})
}),
)
}
但是,我收到以下错误:
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/lib.rs:79:39
|
75 | .and_then(move |(res, file)| {
| ---- captured outer variable
...
79 | io::write_all(file, chunk)
| ^^^^ cannot move out of captured outer variable in an `FnMut` closure
从概念上讲,我理解错误的含义:由于 FnMut
通过可变引用捕获变量,我无法移动捕获的变量。但是,我不明白如何在给出的示例中解决这个问题,因为我需要将流写入 Join
未来返回的文件。
Write
特征中的 write_all
方法可以在这里工作,因为它将文件作为可变引用,但问题是它在同一个线程上进行写入。
您不想使用 for_each
。 io::write_all
consumes the target and the buffer in exchange for a future that will return the target and the buffer when it is done. You can combine this with Stream::fold
重用文件:
.fold(file, |file, chunk| {
io::write_all(file, chunk)
.map(|(f, _c)| f)
.map_err(|_| DownloadFileError::FileWrite)
})
.map(drop)
另请参阅:
我正在尝试创建一个使用 hyper 将远程文件下载到本地文件路径的简单函数。我也需要异步写入文件(在我的例子中,我使用 tokio_fs
)。这是代码:
// Parts of the code were omitted, see the playground for full source code
pub fn download_file(
uri: Uri,
file_location: &Path,
) -> Box<Future<Item = (), Error = DownloadFileError>> {
let temp_dir_path = tempfile::tempdir().unwrap().into_path();
let file_name = match file_location.file_name() {
Some(file_name) => file_name,
None => return Box::new(futures::failed(DownloadFileError::IncorrectFilePath)),
};
let temp_filepath = temp_dir_path.join(&file_name);
let connector = HttpsConnector::new(2).unwrap();
let client: Client<_, Body> = Client::builder().build(connector);
let response_future = client
.get(uri)
.map_err(|err| DownloadFileError::GetRequest(err));
let create_file_future =
File::create(temp_filepath).map_err(|err| DownloadFileError::CreateFile(err));
Box::new(
response_future
.join(create_file_future)
.and_then(move |(res, file)| {
res.into_body()
.map_err(|e| DownloadFileError::GetRequest(e))
.for_each(move |chunk| {
io::write_all(file, chunk)
.map(|_| ())
.map_err(|_| DownloadFileError::FileWrite)
})
}),
)
}
但是,我收到以下错误:
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/lib.rs:79:39
|
75 | .and_then(move |(res, file)| {
| ---- captured outer variable
...
79 | io::write_all(file, chunk)
| ^^^^ cannot move out of captured outer variable in an `FnMut` closure
从概念上讲,我理解错误的含义:由于 FnMut
通过可变引用捕获变量,我无法移动捕获的变量。但是,我不明白如何在给出的示例中解决这个问题,因为我需要将流写入 Join
未来返回的文件。
Write
特征中的 write_all
方法可以在这里工作,因为它将文件作为可变引用,但问题是它在同一个线程上进行写入。
您不想使用 for_each
。 io::write_all
consumes the target and the buffer in exchange for a future that will return the target and the buffer when it is done. You can combine this with Stream::fold
重用文件:
.fold(file, |file, chunk| {
io::write_all(file, chunk)
.map(|(f, _c)| f)
.map_err(|_| DownloadFileError::FileWrite)
})
.map(drop)
另请参阅: