Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a string to S3 using rusoto

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?

like image 963
Explosion Pills Avatar asked Dec 16 '18 02:12

Explosion Pills


People also ask

Which method can be used to upload objects to an S3 bucket?

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.

How many ways you can upload data to S3?

There are three ways in which you can upload a file to amazon S3.


1 Answers

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()
}
like image 175
Shepmaster Avatar answered Sep 21 '22 00:09

Shepmaster