-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added a Java example for creating an S3 Object Lambda #1125
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
// Construct the access point ARN using the region, account ID and access point name | ||
var accessPoint = "arn:aws:s3:" + Aws.REGION + ":" + Aws.ACCOUNT_ID + ":accesspoint/" + S3_ACCESS_POINT_NAME; | ||
|
||
// Create a new S3 bucket with secure configuration including: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including what?
* @param event The S3 Object Lambda event containing request details | ||
* @param context The Lambda execution context | ||
*/ | ||
public void handleRequest(S3ObjectLambdaEvent event, Context context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be replaced by:
public void handleRequest(S3ObjectLambdaEvent event, Context context) throws IOException, InterruptedException {
// the rest of the code
}
The catch block should be removed.
|
||
// Create HTTP client and fetch the original object | ||
var uri = URI.create(s3Url); | ||
var httpClient = HttpClient.newBuilder().build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This client is never closed. This line of code should be removed from here and the client should be created in the statement of the try block. So this code:
try (var s3Client = S3Client.create()) {
// the rest of the code
}
should be replaced with this:
try (
var s3Client = S3Client.create();
var httpClient = HttpClient.newBuilder().build()
) {
// the rest of the code
}
This will ensure that the httpClient is closed automatically.
// Write the final response back to S3 Object Lambda with either transformed | ||
// object or error details | ||
s3Client.writeGetObjectResponse(writeGetObjectResponseRequestBuilder.build(), requestBody); | ||
} catch (IOException | InterruptedException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way it is now, this catch block is useless. It does not actually handle the exceptions caught. So it can be removed altogether. Obviously, the handleRequest method would then have to declare the 2 checked exceptions. Something like this:
public void handleRequest(S3ObjectLambdaEvent event, Context context) throws IOException, InterruptedException {
try (
var s3Client = S3Client.create();
var httpClient = HttpClient.newBuilder().build()
) {
// the rest of the code
} // no more catch block here
Or, the catch block could actually handle the exceptions caught by doing the following:
- log the exception so that it shows up in CloudWatch (this way you have more details later on about what went wrong);
- send a response to the S3 Object Lambda with a 500
statusCode
for Internal Server Error and the correspondingerrorCode
anderrorMessage
.
|
||
/** | ||
* Logs a message to CloudWatch using the Lambda context logger. | ||
* Prefixes the message with the request ID for tracing purposes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is wrong and it should be removed. The message is not prefixed with the request ID. The request ID will not show up in the CloudWatch logs.
Hi @kaiz-io, I believe that some things were overlooked. I don't know how to handle it though since the PR is already merged. However, I figured I should point out what I've noticed. Also, some of the comments are a bit excessive don't you think? I understand this is an example/tutorial but for the most part the code is self-explanatory. Is it really necessary to say that a method called Bye. |
This PR adds a Java example for creating an S3 Object Lambda.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.