From 8c25ceb69ba0ed8b59daa5896e14799565a0b914 Mon Sep 17 00:00:00 2001 From: Alexander Yu Date: Thu, 9 Jan 2025 15:52:22 -0800 Subject: [PATCH 1/2] Added working example-java. Functionally the same as example-go. --- .../example-java/function/build.gradle | 28 +++++++ sample-apps/example-java/function/event.json | 5 ++ sample-apps/example-java/function/output.txt | 1 + sample-apps/example-java/function/pom.xml | 73 +++++++++++++++++++ .../src/main/java/example/OrderHandler.java | 64 ++++++++++++++++ 5 files changed, 171 insertions(+) create mode 100644 sample-apps/example-java/function/build.gradle create mode 100644 sample-apps/example-java/function/event.json create mode 100644 sample-apps/example-java/function/output.txt create mode 100644 sample-apps/example-java/function/pom.xml create mode 100644 sample-apps/example-java/function/src/main/java/example/OrderHandler.java diff --git a/sample-apps/example-java/function/build.gradle b/sample-apps/example-java/function/build.gradle new file mode 100644 index 00000000..ab6730c9 --- /dev/null +++ b/sample-apps/example-java/function/build.gradle @@ -0,0 +1,28 @@ +plugins { + id 'java' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'com.amazonaws:aws-lambda-java-core:1.2.3' + implementation 'software.amazon.awssdk:s3:2.28.29' + implementation 'org.slf4j:slf4j-nop:2.0.16' +} + +task buildZip(type: Zip) { + from compileJava + from processResources + into('lib') { + from configurations.runtimeClasspath + } +} + +java { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} + +build.dependsOn buildZip diff --git a/sample-apps/example-java/function/event.json b/sample-apps/example-java/function/event.json new file mode 100644 index 00000000..fefc63c3 --- /dev/null +++ b/sample-apps/example-java/function/event.json @@ -0,0 +1,5 @@ +{ + "orderId": "1200Gradle", + "amount": 359.99, + "item": "Aviation Headset" +} diff --git a/sample-apps/example-java/function/output.txt b/sample-apps/example-java/function/output.txt new file mode 100644 index 00000000..70d6318b --- /dev/null +++ b/sample-apps/example-java/function/output.txt @@ -0,0 +1 @@ +"Success" \ No newline at end of file diff --git a/sample-apps/example-java/function/pom.xml b/sample-apps/example-java/function/pom.xml new file mode 100644 index 00000000..72c3b301 --- /dev/null +++ b/sample-apps/example-java/function/pom.xml @@ -0,0 +1,73 @@ + + 4.0.0 + com.example + example-java + jar + 1.0-SNAPSHOT + example-java-function + + UTF-8 + 21 + 21 + + + + com.amazonaws + aws-lambda-java-core + 1.2.3 + + + software.amazon.awssdk + s3 + 2.28.29 + + + org.slf4j + slf4j-nop + 2.0.16 + + + + + + + maven-surefire-plugin + 3.5.2 + + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + false + + + *:* + + META-INF/* + META-INF/versions/** + + + + + + + package + + shade + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 21 + + + + + \ No newline at end of file diff --git a/sample-apps/example-java/function/src/main/java/example/OrderHandler.java b/sample-apps/example-java/function/src/main/java/example/OrderHandler.java new file mode 100644 index 00000000..df50eef0 --- /dev/null +++ b/sample-apps/example-java/function/src/main/java/example/OrderHandler.java @@ -0,0 +1,64 @@ +package example; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; +import software.amazon.awssdk.services.s3.model.S3Exception; + +import java.nio.charset.StandardCharsets; + +/** + * Lambda handler for processing orders and storing receipts in S3. + */ +public class OrderHandler implements RequestHandler { + + private static final S3Client S3_CLIENT = S3Client.builder().build(); + + /** + * Record to model the input event. + */ + public record Order(String orderId, double amount, String item) {} + + @Override + public String handleRequest(Order event, Context context) { + try { + // Access environment variables + String bucketName = System.getenv("RECEIPT_BUCKET"); + if (bucketName == null || bucketName.isEmpty()) { + throw new IllegalArgumentException("RECEIPT_BUCKET environment variable is not set"); + } + + // Create the receipt content and key destination + String receiptContent = String.format("OrderID: %s\nAmount: $%.2f\nItem: %s", + event.orderId(), event.amount(), event.item()); + String key = "receipts/" + event.orderId() + ".txt"; + + // Upload the receipt to S3 + uploadReceiptToS3(bucketName, key, receiptContent); + + context.getLogger().log("Successfully processed order " + event.orderId() + + " and stored receipt in S3 bucket " + bucketName); + return "Success"; + + } catch (Exception e) { + context.getLogger().log("Failed to process order: " + e.getMessage()); + throw new RuntimeException(e); + } + } + + private void uploadReceiptToS3(String bucketName, String key, String receiptContent) { + try { + PutObjectRequest putObjectRequest = PutObjectRequest.builder() + .bucket(bucketName) + .key(key) + .build(); + + // Convert the receipt content to bytes and upload to S3 + S3_CLIENT.putObject(putObjectRequest, RequestBody.fromBytes(receiptContent.getBytes(StandardCharsets.UTF_8))); + } catch (S3Exception e) { + throw new RuntimeException("Failed to upload receipt to S3: " + e.awsErrorDetails().errorMessage(), e); + } + } +} From 6c2ed9b489891fe5102c49c1af322b72b29113e0 Mon Sep 17 00:00:00 2001 From: Alexander Yu Date: Wed, 22 Jan 2025 09:55:43 -0800 Subject: [PATCH 2/2] Remove extra files --- sample-apps/example-java/function/event.json | 5 ----- sample-apps/example-java/function/output.txt | 1 - 2 files changed, 6 deletions(-) delete mode 100644 sample-apps/example-java/function/event.json delete mode 100644 sample-apps/example-java/function/output.txt diff --git a/sample-apps/example-java/function/event.json b/sample-apps/example-java/function/event.json deleted file mode 100644 index fefc63c3..00000000 --- a/sample-apps/example-java/function/event.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "orderId": "1200Gradle", - "amount": 359.99, - "item": "Aviation Headset" -} diff --git a/sample-apps/example-java/function/output.txt b/sample-apps/example-java/function/output.txt deleted file mode 100644 index 70d6318b..00000000 --- a/sample-apps/example-java/function/output.txt +++ /dev/null @@ -1 +0,0 @@ -"Success" \ No newline at end of file