Uploading a string to S3 using rusoto
Asked Answered
F

1

11

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?

Flash answered 16/12, 2018 at 2:55 Comment(0)
T
10

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()
}
Thunder answered 16/12, 2018 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.