Rust SDK - Converting S3 JSON to Rust Object inside Lambda

0

Hi there,

I am using Rust for AWS Lambdas and I need to get an object from S3. It is saved as an .json and I need to use this object inside the lambda function as an Rust object.

But how? I came up with the following but It I just don't know how to make it work...

Thank you for any help!

use lambda_runtime::{handler_fn, Context, Error};
use aws_config::load_from_env;
use aws_sdk_s3::Client;
use serde::Deserialize;
use serde_json;
(...)

    let config = load_from_env().await;
    let client = Client::new(&config);

    let bucket_name = "bname";
    let key = "key";

    let response: aws_sdk_s3::operation::get_object::GetObjectOutput = client.get_object()
        .bucket(bucket_name)
        .key(key)
        .send()
        .await?;
nico
질문됨 2달 전160회 조회
2개 답변
1

Step 1: Include Dependencies First, include the necessary dependencies in your Cargo.toml file. You will need the AWS SDK for Rust with S3 client support and serde for JSON handling.

[dependencies]
aws-sdk-s3 = { version = "0.17.0", features = ["full"] } # Check for the latest version
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
lambda_runtime = "0.4.1"

Ensure you're using compatible and latest versions as appropriate.

Step 2: Define Your Data Structure Define a Rust structure that matches the JSON object you expect to work with. Use serde::{Serialize, Deserialize} to easily convert between the JSON text and your Rust structure.

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct MyData {
    // Define your data structure that matches the JSON object
    example_field: String,
    another_field: i32,
}

Step 3: Implement Lambda Handler Implement your Lambda function handler. This function will use the AWS SDK for Rust to download the JSON object from S3 and deserialize it into your Rust structure.

use aws_sdk_s3::{Client, Error};
use lambda_runtime::{handler_fn, Context, Error as LambdaError};
use serde_json::from_str;
use tokio::runtime::Runtime;

async fn get_s3_object(s3_client: &Client, bucket: &str, key: &str) -> Result<String, Error> {
    let resp = s3_client.get_object()
        .bucket(bucket)
        .key(key)
        .send()
        .await?;

    let data = resp.body.collect().await?;
    let content = String::from_utf8(data.into_bytes().to_vec()).expect("Invalid UTF-8");

    Ok(content)
}

async fn function_handler(event: serde_json::Value, _: Context) -> Result<(), LambdaError> {
    let runtime = Runtime::new().unwrap();
    let shared_config = aws_config::load_from_env().await;
    let s3_client = Client::new(&shared_config);

    // Example: Specify your bucket and object key
    let bucket = "your-bucket-name";
    let key = "path/to/your/object.json";

    let object_content = runtime.block_on(get_s3_object(&s3_client, bucket, key))?;
    let my_data: MyData = from_str(&object_content)?;

    // Now `my_data` is available as a Rust object
    println!("{:?}", my_data);

    Ok(())
}

fn main() -> Result<(), LambdaError> {
    let runtime = Runtime::new().unwrap();
    runtime.block_on(lambda_runtime::run(handler_fn(function_handler)))
}
profile picture
전문가
답변함 2달 전
profile picture
전문가
Artem
검토됨 2달 전
1

I solved it using this code:

        let response: aws_sdk_s3::operation::get_object::GetObjectOutput = client.get_object()
            .bucket(bucket_name)
            .key(key)
            .send()
            .await?;
    
        let body = response.body.collect().await?;
        let bytes = body.into_bytes();
        let data: Vec<Vec<f64>> = serde_json::from_slice(&bytes)?; //Data Type has to match
nico
답변함 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠