Skip to content

Commit 8c25ceb

Browse files
committed
Added working example-java. Functionally the same as example-go.
1 parent a2f0355 commit 8c25ceb

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
implementation 'com.amazonaws:aws-lambda-java-core:1.2.3'
11+
implementation 'software.amazon.awssdk:s3:2.28.29'
12+
implementation 'org.slf4j:slf4j-nop:2.0.16'
13+
}
14+
15+
task buildZip(type: Zip) {
16+
from compileJava
17+
from processResources
18+
into('lib') {
19+
from configurations.runtimeClasspath
20+
}
21+
}
22+
23+
java {
24+
sourceCompatibility = JavaVersion.VERSION_21
25+
targetCompatibility = JavaVersion.VERSION_21
26+
}
27+
28+
build.dependsOn buildZip
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"orderId": "1200Gradle",
3+
"amount": 359.99,
4+
"item": "Aviation Headset"
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Success"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>example-java</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>example-java-function</name>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>21</maven.compiler.source>
12+
<maven.compiler.target>21</maven.compiler.target>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.amazonaws</groupId>
17+
<artifactId>aws-lambda-java-core</artifactId>
18+
<version>1.2.3</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>software.amazon.awssdk</groupId>
22+
<artifactId>s3</artifactId>
23+
<version>2.28.29</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.slf4j</groupId>
27+
<artifactId>slf4j-nop</artifactId>
28+
<version>2.0.16</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-surefire-plugin</artifactId>
36+
<version>3.5.2</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-shade-plugin</artifactId>
41+
<version>3.4.1</version>
42+
<configuration>
43+
<createDependencyReducedPom>false</createDependencyReducedPom>
44+
<filters>
45+
<filter>
46+
<artifact>*:*</artifact>
47+
<excludes>
48+
<exclude>META-INF/*</exclude>
49+
<exclude>META-INF/versions/**</exclude>
50+
</excludes>
51+
</filter>
52+
</filters>
53+
</configuration>
54+
<executions>
55+
<execution>
56+
<phase>package</phase>
57+
<goals>
58+
<goal>shade</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<version>3.13.0</version>
67+
<configuration>
68+
<release>21</release>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package example;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.awssdk.core.sync.RequestBody;
6+
import software.amazon.awssdk.services.s3.S3Client;
7+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
8+
import software.amazon.awssdk.services.s3.model.S3Exception;
9+
10+
import java.nio.charset.StandardCharsets;
11+
12+
/**
13+
* Lambda handler for processing orders and storing receipts in S3.
14+
*/
15+
public class OrderHandler implements RequestHandler<OrderHandler.Order, String> {
16+
17+
private static final S3Client S3_CLIENT = S3Client.builder().build();
18+
19+
/**
20+
* Record to model the input event.
21+
*/
22+
public record Order(String orderId, double amount, String item) {}
23+
24+
@Override
25+
public String handleRequest(Order event, Context context) {
26+
try {
27+
// Access environment variables
28+
String bucketName = System.getenv("RECEIPT_BUCKET");
29+
if (bucketName == null || bucketName.isEmpty()) {
30+
throw new IllegalArgumentException("RECEIPT_BUCKET environment variable is not set");
31+
}
32+
33+
// Create the receipt content and key destination
34+
String receiptContent = String.format("OrderID: %s\nAmount: $%.2f\nItem: %s",
35+
event.orderId(), event.amount(), event.item());
36+
String key = "receipts/" + event.orderId() + ".txt";
37+
38+
// Upload the receipt to S3
39+
uploadReceiptToS3(bucketName, key, receiptContent);
40+
41+
context.getLogger().log("Successfully processed order " + event.orderId() +
42+
" and stored receipt in S3 bucket " + bucketName);
43+
return "Success";
44+
45+
} catch (Exception e) {
46+
context.getLogger().log("Failed to process order: " + e.getMessage());
47+
throw new RuntimeException(e);
48+
}
49+
}
50+
51+
private void uploadReceiptToS3(String bucketName, String key, String receiptContent) {
52+
try {
53+
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
54+
.bucket(bucketName)
55+
.key(key)
56+
.build();
57+
58+
// Convert the receipt content to bytes and upload to S3
59+
S3_CLIENT.putObject(putObjectRequest, RequestBody.fromBytes(receiptContent.getBytes(StandardCharsets.UTF_8)));
60+
} catch (S3Exception e) {
61+
throw new RuntimeException("Failed to upload receipt to S3: " + e.awsErrorDetails().errorMessage(), e);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)