in java-app-backend/BackendFunction/src/main/java/com/webapp/function/ImageUploadHandler.java [37:76]
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent();
Map<String, String> headers = new HashMap<>();
headers.put("Access-Control-Allow-Origin", "*");
apiGatewayProxyResponseEvent.withHeaders(headers);
String contentType = input.getQueryStringParameters().getOrDefault("content-type", "");
String fileExtension = input.getQueryStringParameters().getOrDefault("file-extension", "");
if (contentType.isEmpty() || fileExtension.isEmpty()) {
return apiGatewayProxyResponseEvent
.withStatusCode(400)
.withBody("{\n" +
" \"message\": \"Both content-type and file-extension need to passed as query param!\" \n" +
"}");
}
String fileName = UUID.randomUUID() + fileExtension;
String imagePath = "index/static/" + fileName;
String personName = input.getQueryStringParameters().getOrDefault("person-name", "");
LOG.debug("File path to be saved {} in bucket {}", imagePath, S3_BUCKET);
LOG.debug("Received metadata {}", personName);
HashMap<String, String> metaData = new HashMap<>();
metaData.put("fullname", personName);
PresignedPutObjectRequest requestObject = client.presignPutObject(req ->
req.putObjectRequest(obj ->
obj.metadata(metaData)
.bucket(S3_BUCKET)
.key(imagePath)
.contentType(contentType)).signatureDuration(Duration.ofSeconds(60)));
metricsLogger().putMetric("RemainingTime", context.getRemainingTimeInMillis(), Unit.MILLISECONDS);
LOG.debug("Generated pre signed url {}", requestObject.url());
LOG.debug("Generated pre signed details meta {}", requestObject.signedHeaders());
return response(apiGatewayProxyResponseEvent, fileName, requestObject.url());
}