I'm using rusoto S3 to create a JSON string and upload this string to an S3 bucket. I can create the string, but rusoto's S3 PutObjectRequest
requires a StreamingBody
and I'm not sure how I can create a StreamingBody
from a string or whether this is actually necessary.
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");
}
The error I get is
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`
I'm not sure how to give this a ByteStream
... ByteStream::new(json::stringify(paths))
does not work and gives me a different error.
How can I upload a string?
To upload folders and files to an S3 bucket Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.
There are three ways in which you can upload a file to amazon S3.
StreamingBody
is a type alias:
type StreamingBody = ByteStream;
ByteStream
has multiple constructors, including an implementation of From
:
impl From<Vec<u8>> for ByteStream
You can convert a String
into a Vec<u8>
using String::into_bytes
. All together:
fn example(s: String) -> rusoto_s3::StreamingBody {
s.into_bytes().into()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With