An event is a JSON-formatted document that contains data for a Lambda function to process. The runtime converts the event to an object and passes it to your function code. When you invoke a function, you determine the structure and contents of the event.
xxxxxxxxxx
{
"TemperatureK": 281,
"WindKmh": -3,
"HumidityPct": 0.55,
"PressureHPa": 1020
}
{
"Records": [
{
"Sns": {
"Timestamp": "2019-01-02T12:45:07.000Z",
"Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-concepts.html
xxxxxxxxxx
public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {
private static final int IMG_WIDTH = 100;
private static final int IMG_HEIGHT = 100;
private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
private String resizedBucketName = "resizedimgmtdevukcom";
public LambdaFunctionHandler() {}
// Test purpose only.
LambdaFunctionHandler(AmazonS3 s3) {
this.s3 = s3;
}
@Override
public String handleRequest(S3Event event, Context context) {
context.getLogger().log("Received event: " + event);
// Get the object from the event and show its content type
String bucket = event.getRecords().get(0).getS3().getBucket().getName();
String key = event.getRecords().get(0).getS3().getObject().getKey();
try {
S3Object response = s3.getObject(new GetObjectRequest(bucket, key));
String contentType = response.getObjectMetadata().getContentType();
context.getLogger().log("CONTENT TYPE: " + contentType);
resizeImageAndSaveToS3(response);
return contentType;
} catch (Exception e) {
e.printStackTrace();
context.getLogger().log(String.format(
"Error getting object %s from bucket %s. Make sure they exist and"
+ " your bucket is in the same region as this function.", key, bucket));
throw e;
}
}
private void resizeImageAndSaveToS3(S3Object response) {
try (InputStream s3ObjectData = response.getObjectContent()) {
File sourceImageFile = File.createTempFile("sourceImgFile", "");
Files.copy(s3ObjectData, sourceImageFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
BufferedImage originalImage = ImageIO.read(sourceImageFile);
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type);
File resizedImageFile = File.createTempFile("resizedimage", "");
ImageIO.write(resizeImageJpg, "jpg", resizedImageFile);
// Push the image to the other bucket
s3.putObject(new PutObjectRequest(resizedBucketName , response.getKey(), resizedImageFile));
} catch (IOException e) {
e.printStackTrace();
}
}
private BufferedImage resizeImage(BufferedImage originalImage, int type) {
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
}