-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ReflectionsProjections/dev/alex/s3-service
S3 service for resume upload / download
- Loading branch information
Showing
8 changed files
with
8,604 additions
and
39 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { S3 } from "@aws-sdk/client-s3"; | ||
import { Config } from "../config"; | ||
|
||
export function s3ClientMiddleware( | ||
_: Request, | ||
res: Response, | ||
next: NextFunction | ||
): void { | ||
res.locals.s3 = new S3({ | ||
apiVersion: "2006-03-01", | ||
credentials: { | ||
accessKeyId: Config.S3_ACCESS_KEY, | ||
secretAccessKey: Config.S3_SECRET_KEY, | ||
}, | ||
region: Config.S3_REGION, | ||
}); | ||
|
||
return next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Request, Response, Router } from "express"; | ||
import RoleChecker from "../../middleware/role-checker"; | ||
import { s3ClientMiddleware } from "../../middleware/s3"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { Config } from "../../config"; | ||
import { Role } from "../auth/auth-models"; | ||
|
||
import { GetObjectCommand, S3 } from "@aws-sdk/client-s3"; | ||
import { createPresignedPost } from "@aws-sdk/s3-presigned-post"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
|
||
const s3Router: Router = Router(); | ||
|
||
s3Router.get( | ||
"/upload/", | ||
RoleChecker([Role.enum.USER], false), | ||
s3ClientMiddleware, | ||
async (_req: Request, res: Response) => { | ||
const payload = res.locals.payload; | ||
|
||
const s3 = res.locals.s3 as S3; | ||
const userId: string = payload.userId; | ||
|
||
const { url, fields } = await createPresignedPost(s3, { | ||
Bucket: Config.S3_BUCKET_NAME, | ||
Key: `${userId}.pdf`, | ||
Conditions: [ | ||
["content-length-range", 0, Config.MAX_RESUME_SIZE_BYTES], // 6 MB max | ||
], | ||
Fields: { | ||
success_action_status: "201", | ||
"Content-Type": "application/pdf", | ||
}, | ||
Expires: Config.RESUME_URL_EXPIRY_SECONDS, | ||
}); | ||
|
||
return res.status(StatusCodes.OK).send({ url: url, fields: fields }); | ||
} | ||
); | ||
|
||
s3Router.get( | ||
"/download/", | ||
RoleChecker([Role.enum.USER], false), | ||
s3ClientMiddleware, | ||
async (_req: Request, res: Response) => { | ||
const payload = res.locals.payload; | ||
|
||
const s3 = res.locals.s3 as S3; | ||
const userId: string = payload.userId; | ||
|
||
const command = new GetObjectCommand({ | ||
Bucket: Config.S3_BUCKET_NAME, | ||
Key: `${userId}.pdf`, | ||
}); | ||
|
||
const downloadUrl = await getSignedUrl(s3, command, { | ||
expiresIn: Config.RESUME_URL_EXPIRY_SECONDS, | ||
}); | ||
|
||
return res.status(StatusCodes.OK).send({ url: downloadUrl }); | ||
} | ||
); | ||
|
||
s3Router.get( | ||
"/download/:USERID", | ||
RoleChecker([Role.enum.ADMIN], false), | ||
s3ClientMiddleware, | ||
async (req: Request, res: Response) => { | ||
const userId: string = req.params.USERID; | ||
const s3 = res.locals.s3 as S3; | ||
|
||
const command = new GetObjectCommand({ | ||
Bucket: Config.S3_BUCKET_NAME, | ||
Key: `${userId}.pdf`, | ||
}); | ||
|
||
const downloadUrl = await getSignedUrl(s3, command, { | ||
expiresIn: Config.RESUME_URL_EXPIRY_SECONDS, | ||
}); | ||
|
||
return res.status(StatusCodes.OK).send({ url: downloadUrl }); | ||
} | ||
); | ||
|
||
export default s3Router; |
Oops, something went wrong.