amazon-s3 - 使用 rusoto 将字符串上传到 S3

标签 amazon-s3 rust type-conversion 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 有多个构造函数,包括 implementation of From :

impl From<Vec<u8>> for ByteStream

您可以转换 String进入 Vec<u8>使用 String::into_bytes .一起:

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

关于amazon-s3 - 使用 rusoto 将字符串上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799059/

相关文章:

amazon-web-services - Terraform AWS S3 - 拒绝除特定用户以外的所有人

导入时出现 Rust "expected identifier, found keyword"

testing - 如何处理将可变借用的结构传递给函数闭包?

java - System.currentTimeMillis() 转 int 返回负值

java - Java 中修复捕获转换的正确方法

scala - 无法在 Redshift 中保存数据框

android - 没有互联网连接时应用程序崩溃

audio - 跨站点 swf 加载和播放 mp3

c - 不安全的转换

macos - 为什么这个 OpenGL 应用程序在某些 Mac 上运行得非常慢?