我正在使用 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/