Skip to content

Commit

Permalink
Updates image submission handler to use node20 and updated aws-sdk li…
Browse files Browse the repository at this point in the history
…brary
  • Loading branch information
gcardonag committed Jan 27, 2024
1 parent 7a7ccaa commit 98ea5de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lambda-image_submission_handler.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ resource "aws_lambda_function" "image_submission_handler" {

source_code_hash = filebase64sha256(data.archive_file.image_submission_handler.output_path)

runtime = "nodejs12.x"
runtime = "nodejs20.x"

tags = {
Terraform = true
Expand Down
31 changes: 10 additions & 21 deletions src_code/image-submission-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const uuid = require('uuid');
const querystring = require('querystring');

const bucket = "phlask-tap-images"
const AWS = require("aws-sdk")
var s3 = new AWS.S3({
signatureVersion: 'v4',
region: 'us-east-2'
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const { PutObjectCommand, S3 } = require('@aws-sdk/client-s3');

var s3 = new S3({
region: 'us-east-2'
});

exports.handler = (event, context, callback) => {
exports.handler = async(event, context, callback) => {
console.log(JSON.stringify(event, null, 4));
const request = event.Records[0].cf.request;

Expand All @@ -34,19 +35,6 @@ exports.handler = (event, context, callback) => {
var imageID = uuid.v4();

var prefix = ''
/* TODO: Update the method to get the host header based on the following request content
{
"request": {
"clientIp": "173.62.214.14",
"headers": {
"host": [
{
"key": "Host",
"value": "beta.phlask.me"
}
],
*/
var hostname = request.headers.host[0].value
if (hostname == "test.phlask.me") {
prefix = 'test/'
Expand All @@ -58,7 +46,7 @@ exports.handler = (event, context, callback) => {
prefix = 'prod/'
}
else {
console.log('The request header was invalid: ', request.headers.host.value)
console.log('The request header was invalid: ', request.headers.host[0].value)
return callback(null, {
body: JSON.stringify({}),
status: '400',
Expand All @@ -69,15 +57,16 @@ exports.handler = (event, context, callback) => {
var putParams = {
Bucket: bucket,
Key: prefix + 'tap-images/' + imageID + '.' + type,
Expires: 300
};

// var getParams = {
// Bucket: bucket,
// Key: imageID + '.' + type
// };

var putURL = s3.getSignedUrl('putObject', putParams)
var putURL = await getSignedUrl(s3, new PutObjectCommand(putParams), {
expiresIn: 300
})
/* TODO: Adjust the GET URL to allow getting images at the expected endpoint */
// var getURL = s3.getSignedUrl('getObject', getParams)
const getURL = imageID + '.' + type
Expand Down
12 changes: 12 additions & 0 deletions src_code/image-submission-handler/localTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//import your handler file or main file of Lambda
let index = require('./index');

//Call your exports function with required params
//In AWS lambda these are event, content, and callback
//event and content are JSON object and callback is a function
//In my example i'm using empty JSON
index.handler( require('./testEvent.json'), //event
{}, //content
function(ss,data) { //callback function with two arguments
console.log(data);
});
20 changes: 20 additions & 0 deletions src_code/image-submission-handler/testEvent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Records": [
{
"cf": {
"request": {
"headers": {
"host": [
{
"key": "Host",
"value": "phlask.me"
}
]
},
"method": "GET",
"querystring": "type=image/png"
}
}
}
]
}

0 comments on commit 98ea5de

Please sign in to comment.