Skip to content

Commit ea8d8a2

Browse files
committed
Upgraded the s3-java sample to use the latest dependency versions, most notably the AWS SDK for Java 2.x.
- Refactored the Handler and test code to work with version 2.x of the SDK. - Updated both build.gradle and pom.xml files to use latest dependencies. - Minor edit to 5-invoke.sh script which was complaining about Invalid base64. - Tested the tutorial (scripts 1 through 6) end to end and it still works.
1 parent 4c88540 commit ea8d8a2

File tree

5 files changed

+84
-66
lines changed

5 files changed

+84
-66
lines changed

sample-apps/s3-java/5-invoke.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [ ! -f event.json ]; then
99
1010
fi
1111
while true; do
12-
aws lambda invoke --function-name $FUNCTION --payload file://event.json out.json
12+
aws lambda invoke --function-name $FUNCTION --payload file://event.json out.json --cli-binary-format raw-in-base64-out
1313
cat out.json
1414
echo ""
1515
sleep 2

sample-apps/s3-java/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ repositories {
77
}
88

99
dependencies {
10-
implementation platform('com.amazonaws:aws-xray-recorder-sdk-bom:2.4.0')
10+
implementation platform('software.amazon.awssdk:bom:2.15.0')
11+
implementation platform('com.amazonaws:aws-xray-recorder-sdk-bom:2.11.0')
12+
implementation 'software.amazon.awssdk:s3'
1113
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
12-
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
13-
implementation 'com.amazonaws:aws-java-sdk-s3:1.11.578'
14+
implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'
15+
implementation 'org.apache.logging.log4j:log4j-api:[2.17.1,)'
16+
implementation 'org.apache.logging.log4j:log4j-core:[2.17.1,)'
17+
implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:[2.17.1,)'
18+
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'
1419
implementation 'com.amazonaws:aws-xray-recorder-sdk-core'
1520
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk'
1621
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-instrumentor'
1722
implementation 'com.google.code.gson:gson:2.8.6'
18-
implementation 'org.apache.logging.log4j:log4j-api:[2.17.1,)'
19-
implementation 'org.apache.logging.log4j:log4j-core:[2.17.1,)'
20-
implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:[2.17.1,)'
21-
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.0'
2223
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
2324
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
2425
}

sample-apps/s3-java/pom.xml

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,45 @@
1111
<maven.compiler.source>1.8</maven.compiler.source>
1212
<maven.compiler.target>1.8</maven.compiler.target>
1313
</properties>
14+
15+
<dependencyManagement>
16+
<dependencies>
17+
<dependency>
18+
<groupId>software.amazon.awssdk</groupId>
19+
<artifactId>bom</artifactId>
20+
<version>2.16.1</version>
21+
<type>pom</type>
22+
<scope>import</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.amazonaws</groupId>
26+
<artifactId>aws-xray-recorder-sdk-bom</artifactId>
27+
<version>2.11.0</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
1434
<dependencies>
35+
<dependency>
36+
<groupId>software.amazon.awssdk</groupId>
37+
<artifactId>s3</artifactId>
38+
</dependency>
1539
<dependency>
1640
<groupId>com.amazonaws</groupId>
1741
<artifactId>aws-lambda-java-core</artifactId>
18-
<version>1.2.0</version>
42+
<version>1.2.1</version>
1943
</dependency>
2044
<dependency>
2145
<groupId>com.amazonaws</groupId>
2246
<artifactId>aws-lambda-java-events</artifactId>
23-
<version>2.2.7</version>
47+
<version>3.11.0</version>
2448
</dependency>
2549
<dependency>
2650
<groupId>com.amazonaws</groupId>
2751
<artifactId>aws-lambda-java-log4j2</artifactId>
28-
<version>1.5.0</version>
29-
</dependency>
30-
<dependency>
31-
<groupId>com.google.code.gson</groupId>
32-
<artifactId>gson</artifactId>
33-
<version>2.8.6</version>
52+
<version>1.5.1</version>
3453
</dependency>
3554
<dependency>
3655
<groupId>org.apache.logging.log4j</groupId>
@@ -47,30 +66,22 @@
4766
<artifactId>log4j-slf4j18-impl</artifactId>
4867
<version>[2.17.1,)</version>
4968
</dependency>
50-
<dependency>
51-
<groupId>com.amazonaws</groupId>
52-
<artifactId>aws-java-sdk-s3</artifactId>
53-
<version>1.11.578</version>
54-
</dependency>
5569
<dependency>
5670
<groupId>com.amazonaws</groupId>
5771
<artifactId>aws-xray-recorder-sdk-core</artifactId>
58-
<version>2.4.0</version>
59-
</dependency>
60-
<dependency>
61-
<groupId>com.amazonaws</groupId>
62-
<artifactId>aws-xray-recorder-sdk-aws-sdk-core</artifactId>
63-
<version>2.4.0</version>
6472
</dependency>
6573
<dependency>
6674
<groupId>com.amazonaws</groupId>
6775
<artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
68-
<version>2.4.0</version>
6976
</dependency>
7077
<dependency>
7178
<groupId>com.amazonaws</groupId>
7279
<artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
73-
<version>2.4.0</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.google.code.gson</groupId>
83+
<artifactId>gson</artifactId>
84+
<version>2.8.6</version>
7485
</dependency>
7586
<dependency>
7687
<groupId>org.junit.jupiter</groupId>

sample-apps/s3-java/src/main/java/example/Handler.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@
88
import java.io.ByteArrayOutputStream;
99
import java.io.IOException;
1010
import java.io.InputStream;
11+
import java.util.HashMap;
12+
import java.util.Map;
1113
import java.util.regex.Matcher;
1214
import java.util.regex.Pattern;
1315

1416
import javax.imageio.ImageIO;
1517

16-
import com.amazonaws.AmazonServiceException;
18+
import software.amazon.awssdk.awscore.exception.AwsServiceException;
19+
import software.amazon.awssdk.core.sync.RequestBody;
20+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
21+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
22+
import software.amazon.awssdk.services.s3.model.S3Object;
23+
import software.amazon.awssdk.services.s3.S3Client;
24+
1725
import com.amazonaws.services.lambda.runtime.Context;
1826
import com.amazonaws.services.lambda.runtime.RequestHandler;
1927
import com.amazonaws.services.lambda.runtime.events.S3Event;
20-
import com.amazonaws.services.s3.AmazonS3;
21-
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
22-
import com.amazonaws.services.s3.model.GetObjectRequest;
23-
import com.amazonaws.services.s3.model.ObjectMetadata;
24-
import com.amazonaws.services.s3.model.S3Object;
25-
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
28+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.S3EventNotificationRecord;
2629

2730
import com.google.gson.Gson;
2831
import com.google.gson.GsonBuilder;
@@ -67,19 +70,20 @@ public String handleRequest(S3Event s3event, Context context) {
6770
}
6871

6972
// Download the image from S3 into a stream
70-
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
71-
S3Object s3Object = s3Client.getObject(new GetObjectRequest(
72-
srcBucket, srcKey));
73-
InputStream objectData = s3Object.getObjectContent();
73+
S3Client s3Client = S3Client.builder().build();
74+
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
75+
.bucket(srcBucket)
76+
.key(srcKey)
77+
.build();
78+
InputStream s3Object = s3Client.getObject(getObjectRequest);
7479

7580
// Read the source image
76-
BufferedImage srcImage = ImageIO.read(objectData);
81+
BufferedImage srcImage = ImageIO.read(s3Object);
7782
int srcHeight = srcImage.getHeight();
7883
int srcWidth = srcImage.getWidth();
79-
// Infer the scaling factor to avoid stretching the image
80-
// unnaturally
81-
float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT
82-
/ srcHeight);
84+
// Infer scaling factor to avoid stretching image unnaturally
85+
float scalingFactor = Math.min(
86+
MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight);
8387
int width = (int) (scalingFactor * srcWidth);
8488
int height = (int) (scalingFactor * srcHeight);
8589

@@ -98,25 +102,29 @@ public String handleRequest(S3Event s3event, Context context) {
98102
// Re-encode image to target format
99103
ByteArrayOutputStream os = new ByteArrayOutputStream();
100104
ImageIO.write(resizedImage, imageType, os);
101-
InputStream is = new ByteArrayInputStream(os.toByteArray());
102-
// Set Content-Length and Content-Type
103-
ObjectMetadata meta = new ObjectMetadata();
104-
meta.setContentLength(os.size());
105+
// InputStream is = new ByteArrayInputStream(os.toByteArray());
106+
107+
Map<String, String> metadata = new HashMap<>();
108+
metadata.put("Content-Length", Integer.toString(os.size()));
105109
if (JPG_TYPE.equals(imageType)) {
106-
meta.setContentType(JPG_MIME);
107-
}
108-
if (PNG_TYPE.equals(imageType)) {
109-
meta.setContentType(PNG_MIME);
110+
metadata.put("Content-Type", JPG_MIME);
111+
} else if (PNG_TYPE.equals(imageType)) {
112+
metadata.put("Content-Type", PNG_MIME);
110113
}
114+
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
115+
.bucket(dstBucket)
116+
.key(dstKey)
117+
.metadata(metadata)
118+
.build();
111119

112120
// Uploading to S3 destination bucket
113121
logger.info("Writing to: " + dstBucket + "/" + dstKey);
114122
try {
115-
s3Client.putObject(dstBucket, dstKey, is, meta);
123+
s3Client.putObject(putObjectRequest, RequestBody.fromBytes(os.toByteArray()));
116124
}
117-
catch(AmazonServiceException e)
125+
catch(AwsServiceException e)
118126
{
119-
logger.error(e.getErrorMessage());
127+
logger.error(e.awsErrorDetails().errorMessage());
120128
System.exit(1);
121129
}
122130
logger.info("Successfully resized " + srcBucket + "/"

sample-apps/s3-java/src/test/java/example/InvokeTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
import com.amazonaws.services.lambda.runtime.LambdaLogger;
99
import com.amazonaws.services.lambda.runtime.RequestHandler;
1010
import com.amazonaws.services.lambda.runtime.events.S3Event;
11-
import com.amazonaws.services.s3.event.S3EventNotification;
12-
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
13-
import com.amazonaws.services.s3.event.S3EventNotification.RequestParametersEntity;
14-
import com.amazonaws.services.s3.event.S3EventNotification.ResponseElementsEntity;
15-
import com.amazonaws.services.s3.event.S3EventNotification.S3Entity;
16-
import com.amazonaws.services.s3.event.S3EventNotification.UserIdentityEntity;
17-
import com.amazonaws.services.s3.event.S3EventNotification.GlacierEventDataEntity;
18-
import com.amazonaws.services.s3.event.S3EventNotification.S3BucketEntity;
19-
import com.amazonaws.services.s3.event.S3EventNotification.S3ObjectEntity;
20-
import com.amazonaws.services.s3.event.S3EventNotification.UserIdentityEntity;
11+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
12+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.RequestParametersEntity;
13+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.ResponseElementsEntity;
14+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.S3BucketEntity;
15+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.S3Entity;
16+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.S3EventNotificationRecord;
17+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.S3ObjectEntity;
18+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification.UserIdentityEntity;
2119

2220
import org.slf4j.Logger;
2321
import org.slf4j.LoggerFactory;

0 commit comments

Comments
 (0)