Skip to content
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

Merged
merged 5 commits into from
Jan 9, 2025

Conversation

sobrab
Copy link
Contributor

@sobrab sobrab commented Jan 7, 2025

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.

Copy link
Contributor

@kaiz-io kaiz-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@kaiz-io kaiz-io merged commit cbbbd80 into aws-samples:main Jan 9, 2025
6 checks passed
// 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:
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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();
Copy link
Contributor Author

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) {
Copy link
Contributor Author

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 corresponding errorCode and errorMessage.


/**
* Logs a message to CloudWatch using the Lambda context logger.
* Prefixes the message with the request ID for tracing purposes.
Copy link
Contributor Author

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.

@sobrab
Copy link
Contributor Author

sobrab commented Jan 13, 2025

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 md5Hex will calculate the MD5 hash? Or that a method called runtime will set the runtime? Perhaps all the comments should be double-checked. Anyway, just my 2 cents. Hope you don't mind. And please, do let me know if you need anything else from me related to this PR.

Bye.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants