From c9831bf9ed35ad8351115a9ba0090485f69a15b7 Mon Sep 17 00:00:00 2001 From: Jacob Chang Date: Mon, 15 Jul 2024 00:08:17 -0700 Subject: [PATCH] add enum for bucket name --- src/config.ts | 7 ++++++- src/services/s3/s3-router.ts | 21 +++++++++++++++++---- src/services/s3/s3-utils.ts | 18 +++++++++++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index 259f97a..be4d15e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,6 +9,12 @@ export const Environment = z.enum(["PRODUCTION", "DEVELOPMENT", "TESTING"]); export const MailingListName = z.enum(["rp_interest"]); +// Native enum for bucket names +export enum BucketName { + RP_2024_RESUMES = "rp-2024-resumes", + RP_2024_SPEAKERS = "rp-2024-speakers", +} + export const Config = { DEFAULT_APP_PORT: 3000, ALLOWED_CORS_ORIGIN_PATTERNS: [ @@ -71,7 +77,6 @@ export const Config = { S3_ACCESS_KEY: getEnv("S3_ACCESS_KEY"), S3_SECRET_KEY: getEnv("S3_SECRET_KEY"), - S3_BUCKET_NAME: getEnv("S3_BUCKET_NAME"), S3_REGION: getEnv("S3_REGION"), MAX_RESUME_SIZE_BYTES: 6 * 1024 * 1024, RESUME_URL_EXPIRY_SECONDS: 60, diff --git a/src/services/s3/s3-router.ts b/src/services/s3/s3-router.ts index 9902c8a..64cf466 100644 --- a/src/services/s3/s3-router.ts +++ b/src/services/s3/s3-router.ts @@ -7,6 +7,7 @@ import { Role } from "../auth/auth-models"; import { S3 } from "@aws-sdk/client-s3"; import { getResumeUrl, postResumeUrl } from "./s3-utils"; import BatchResumeDownloadValidator from "./s3-schema"; +import { BucketName } from "../../config"; const s3Router: Router = Router(); @@ -21,7 +22,11 @@ s3Router.get( const userId: string = payload.userId; try { - const { url, fields } = await postResumeUrl(userId, s3); + const { url, fields } = await postResumeUrl( + userId, + s3, + BucketName.RP_2024_RESUMES + ); return res.status(StatusCodes.OK).send({ url, fields }); } catch (error) { next(error); @@ -40,7 +45,11 @@ s3Router.get( const s3 = res.locals.s3 as S3; try { - const downloadUrl = await getResumeUrl(userId, s3); + const downloadUrl = await getResumeUrl( + userId, + s3, + BucketName.RP_2024_RESUMES + ); return res.status(StatusCodes.OK).send({ url: downloadUrl }); } catch (error) { next(error); @@ -57,7 +66,11 @@ s3Router.get( const s3 = res.locals.s3 as S3; try { - const downloadUrl = await getResumeUrl(userId, s3); + const downloadUrl = await getResumeUrl( + userId, + s3, + BucketName.RP_2024_RESUMES + ); return res.status(StatusCodes.OK).send({ url: downloadUrl }); } catch (error) { next(error); @@ -76,7 +89,7 @@ s3Router.get( const { userIds } = BatchResumeDownloadValidator.parse(req.body); const batchDownloadPromises = userIds.map((userId) => - getResumeUrl(userId, s3) + getResumeUrl(userId, s3, BucketName.RP_2024_RESUMES) .then((url) => ({ userId, url: url })) .catch(() => ({ userId, url: null })) ); diff --git a/src/services/s3/s3-utils.ts b/src/services/s3/s3-utils.ts index c6eb2da..2b39811 100644 --- a/src/services/s3/s3-utils.ts +++ b/src/services/s3/s3-utils.ts @@ -1,11 +1,15 @@ import { GetObjectCommand, S3 } from "@aws-sdk/client-s3"; -import Config from "../../config"; +import Config, { BucketName } from "../../config"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; import { createPresignedPost } from "@aws-sdk/s3-presigned-post"; -export async function postResumeUrl(userId: string, client: S3) { +export async function postResumeUrl( + userId: string, + client: S3, + bucketName: BucketName +) { const { url, fields } = await createPresignedPost(client, { - Bucket: Config.S3_BUCKET_NAME, + Bucket: bucketName, Key: `${userId}.pdf`, Conditions: [ ["content-length-range", 0, Config.MAX_RESUME_SIZE_BYTES], // 6 MB max @@ -20,9 +24,13 @@ export async function postResumeUrl(userId: string, client: S3) { return { url, fields }; } -export async function getResumeUrl(userId: string, client: S3) { +export async function getResumeUrl( + userId: string, + client: S3, + bucketName: BucketName +) { const command = new GetObjectCommand({ - Bucket: Config.S3_BUCKET_NAME, + Bucket: bucketName, Key: `${userId}.pdf`, });