3

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

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)对象保存到文件中?

4

2 回答 2

10

Rusoto 现在使用标准库 futures 并且不再提供该sync方法,因此之前的答案不再有效。

读到记忆

use futures::stream::TryStreamExt;
use rusoto_core::Region;
use rusoto_s3::{GetObjectRequest, S3Client, S3};

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

const BUCKET_NAME: &str = "my very own bucket name";

#[tokio::main]
async fn main() -> Result<()> {
    let client = S3Client::new(Region::UsEast2);

    let mut object = client
        .get_object(GetObjectRequest {
            bucket: BUCKET_NAME.into(),
            ..Default::default()
        })
        .await?;

    let body = object.body.take().expect("The object has no body");

    let body = body.map_ok(|b| b.to_vec()).try_concat().await?;
    println!("body length: {}", body.len());

    Ok(())
}

AWS_ACCESS_KEY_ID并且AWS_SECRET_ACCESS_KEY需要指定。我选择在代码之外设置环境变量。

流式传输到文件

use rusoto_core::Region;
use rusoto_s3::{GetObjectRequest, S3Client, S3};
use tokio::{fs::File, io};

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

const BUCKET_NAME: &str = "my very own bucket name";

#[tokio::main]
async fn main() -> Result<()> {
    let client = S3Client::new(Region::UsEast2);

    let mut object = client
        .get_object(GetObjectRequest {
            bucket: BUCKET_NAME.into(),
            ..Default::default()
        })
        .await?;

    let body = object.body.take().expect("The object has no body");

    let mut body = body.into_async_read();
    let mut file = File::create("/tmp/a-place-to-write").await?;
    io::copy(&mut body, &mut file).await?;

    Ok(())
}

虽然ByteStream有一个诱人的into_blocking_read方法,但我不建议使用它。如果你尝试在异步上下文中使用它,你会感到恐慌,因为它会启动一个嵌套的 Tokio 执行器。如果您在异步上下文之外使用它,它将截断数据,除非您非常小心地使用异步运行时但不在其中

也可以看看:

依赖版本

[dependencies]
rusoto_s3 = "0.43.0"
rusoto_core = "0.43.0"
tokio = { version = "0.2.21", features = ["macros"] }
futures = "0.3.5"
于 2020-05-25T13:41:35.257 回答
4

您快到了。您的代码会将对象body放入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");
于 2018-07-12T17:09:25.320 回答