Exp*_*lls 9 type-conversion amazon-s3 rust rusoto
我正在使用rusoto S3 创建一个 JSON 字符串并将此字符串上传到 S3 存储桶。我可以创建字符串,但 rusoto 的 S3PutObjectRequest需要 a StreamingBody,我不确定如何StreamingBody从字符串创建 a ,或者这是否真的有必要。
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");
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
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");
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何给它一个ByteStream......ByteStream::new(json::stringify(paths))不起作用并给我一个不同的错误。
如何上传字符串?
StreamingBody 是一个类型别名:
type StreamingBody = ByteStream;
Run Code Online (Sandbox Code Playgroud)
ByteStream有多个构造函数,包括一个实现From:
impl From<Vec<u8>> for ByteStream
Run Code Online (Sandbox Code Playgroud)
您可以将 aString转换为Vec<u8>using String::into_bytes。全部一起:
fn example(s: String) -> rusoto_s3::StreamingBody {
s.into_bytes().into()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2433 次 |
| 最近记录: |