使用 rusoto 将字符串上传到 S3

Uploading a string to S3 using rusoto

我正在使用 rusoto S3 创建一个 JSON 字符串并将该字符串上传到 S3 存储桶。我可以创建字符串,但是 rusoto 的 S3 PutObjectRequest 需要 StreamingBody,我不确定如何从字符串创建 StreamingBody 或者这是否真的有必要。

extern crate json;
extern crate rusoto_core;
extern crate rusoto_s3;
extern crate futures;

use rusoto_core::Region;
use rusoto_s3::{S3, S3Client, PutObjectRequest};

fn main() {
    let mut paths = Vec::new();
    paths.push(1);
    let s3_client = S3Client::new(Region::UsEast1);
    println!("{}", json::stringify(paths));
    s3_client.put_object(PutObjectRequest {
        bucket: String::from("bucket"),
        key: "@types.json".to_string(),
        body: Some(json::stringify(paths)),
        acl: Some("public-read".to_string()),
        ..Default::default()
    }).sync().expect("could not upload");
}

我得到的错误是

error[E0308]: mismatched types
  --> src/main.rs:16:20
   |
16 |         body: Some(json::stringify(paths)),
   |                    ^^^^^^^^^^^^^^^^^^^^^^ expected struct `rusoto_core::ByteStream`, found struct `std::string::String`
   |
   = note: expected type `rusoto_core::ByteStream`
              found type `std::string::String`

我不确定如何给这个 ByteStream... ByteStream::new(json::stringify(paths)) 不起作用并给我一个不同的错误。

如何上传字符串?

StreamingBody 是类型别名:

type StreamingBody = ByteStream;

ByteStream has multiple constructors, including an implementation of From:

impl From<Vec<u8>> for ByteStream

您可以使用 String::into_bytesString 转换为 Vec<u8>。总计:

fn example(s: String) -> rusoto_s3::StreamingBody {
    s.into_bytes().into()
}