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?;
  • Please accept the answer if it was useful for you

nico
gefragt vor 6 Monaten312 Aufrufe
2 Antworten
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
EXPERTE
beantwortet vor 6 Monaten
profile picture
EXPERTE
Artem
überprüft vor 6 Monaten
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
beantwortet vor 6 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen