How to save a file downloaded from S3 with Rusoto to my hard drive?

為{幸葍}努か 提交于 2019-12-02 02:46:41

问题


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);
}

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


回答1:


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

To write the contents of body to a file:

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");


来源:https://stackoverflow.com/questions/51287360/how-to-save-a-file-downloaded-from-s3-with-rusoto-to-my-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!