簡體   English   中英

使用rusoto將字符串上載到S3

[英]Uploading a string to S3 using rusoto

我正在使用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有多個構造函數,包括From實現

impl From<Vec<u8>> for ByteStream

您可以使用String::into_bytesString轉換為Vec<u8> 全部一起:

fn example(s: String) -> rusoto_s3::StreamingBody {
    s.into_bytes().into()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM