Skip to content

Commit

Permalink
Updated to Node20.x and aws sdk v3
Browse files Browse the repository at this point in the history
  • Loading branch information
shaiq-dev committed Mar 10, 2024
1 parent 86ffafa commit fc1bc02
Show file tree
Hide file tree
Showing 3 changed files with 2,882 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lambda/handlers/resume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { S3Client, ListObjectsCommand, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

export const handler = async (event, context) => {
try {

const allowedOrigins = process.env.ALLOWED_ORIGINS.split(",") || []
const origin = event.headers.Origin || event.headers.origin;

if (!allowedOrigins.includes(origin)) {
return {
statusCode: 401,
body: JSON.stringify({
status: 'ACCESS_DENIED',
}),
}
}

const s3 = new S3Client();
const bucket = process.env.BUCKET

const data = await s3.send(new ListObjectsCommand({
Bucket: bucket,
Prefix: "resume/"
}))

// Sort the contents based on date
const contents = data.Contents.sort((a, b) => {
return a.LastModified.getTime() > b.LastModified.getTime() ? -1 : 1
})

const artifact = contents[0]
const version = artifact.Key.split('-')[1].slice(0, -4)

const url = await getSignedUrl(s3, new GetObjectCommand({
Bucket: bucket,
Key: artifact.Key,
Expires: 600,
}));

return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({
status: 'OK',
version,
url,
}),
}
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({
status: 'ERROR',
}),
}
}
}
Loading

0 comments on commit fc1bc02

Please sign in to comment.