如何将使用Rusoto从S3下载的文件保存到我的硬盘上? [英] How to save a file downloaded from S3 with Rusoto to my hard drive?

查看:171
本文介绍了如何将使用Rusoto从S3下载的文件保存到我的硬盘上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Rusoto从存储桶中下载文件,并且正在获取文件内容:

I am trying to download a file from a bucket with Rusoto and I am getting the file content:

fn get_object(client: &TestClient, bucket: &str, filename: &str) {
    let get_req = GetObjectRequest {
        bucket: bucket.to_owned(),
        key: filename.to_owned(),
        ..Default::default()
    };

    let result = client.get_object(&get_req).sync().expect("Couldn't GET object");


    let stream = result.body.unwrap();
    let body = stream.concat2().wait().unwrap();

    assert!(body.len() > 0);
}

如何将该GetObjectOutput(result)对象保存到文件中?

How can I save this GetObjectOutput(result) object to a file?

推荐答案

您快到了.您的代码会将对象放入body,即Vec<u8>.

You're almost there. Your code will put the object in body, which is a Vec<u8>.

要将body的内容写入文件:

use std::io::Write;
use std::fs::File;

let mut file = File::create("/path/to/my-object").expect("create failed");
file.write_all(&body).expect("failed to write body");

这篇关于如何将使用Rusoto从S3下载的文件保存到我的硬盘上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆