简体   繁体   中英

Rusoto S3 reading object from S3

apologies if this is a dumb question, I am new enough to using rust and i am having a hard time figuring out how to read an object from s3 using the rusoto library.

https://rusoto.github.io/rusoto/rusoto_s3/

so far i've worked out this much:

let mut get_object_request = rusoto_s3::GetObjectRequest::default();
get_object_request.bucket = bucket.to_owned();
get_object_request.key = object_id.to_owned();

let resp = client.get_object(get_object_request)
    .await
    .map_err(|_| {
        FailedToParseFile(format!(
            "Failed to retrieve file from s3, bucket: {}, object id: {}",
            bucket, object_id
        ))
    })?;

let mut resp_body = match resp.body {
    Some(body) => {body}
    None => { return Err(ClientError::FailedToObtainConfiguredFile(
        format!("Failed to retrieve the file: {} in {}", object_id, bucket)
    ))}
};

However i've no idea how to turn the streaming body returned from this into a usable string. I've tried a few things to get this working but none seem to work for me.

let mut byte_buffer = String::new();
    resp_body.into_blocking_read()
        .read_to_string(&mut byte_buffer)
        .map_err(|_| {
            FailedToParseFile(format!(
                "Failed to parse the received file, bucket: {}, object id: {}",
                bucket, object_id
            ))
        })?;

    let resp_body_bytes = byte_buffer.into_bytes();

As i am using tokio (rocket), this blocking queue doesn't seem to be an option for me causing it to panic on attempting to block on async thread. I started looking at the method 'into_async_read()' rather than blocking but i'm unfamiliar with this too and seem to be struggling using it as intended, this is starting to seem pretty convoluted maybe i'm missing something, any help with this would be appreciated.

    let mut byte_buffer = String::new();
    Pin::new(&mut resp_body.into_async_read())
            .read_to_string(&mut byte_buffer)
            .await;

This actually seems to do what I need it to, messing about with this I didn't realise the result of .read_to_string(&mut byte_buffer) could be awaited in order to fill the supplied buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM