Search code examples
fileamazon-s3rustfile-transferrusoto

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


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?


Solution

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