Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
lovebox-lambda/src/presignedUrl.ts /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (41 sloc)
1.21 KB
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
| import { v4 as uuidv4 } from "uuid"; | |
| import { S3 } from "aws-sdk"; | |
| import { APIGatewayEvent } from "aws-lambda"; | |
| const s3 = new S3({ | |
| apiVersion: "2006-03-01", | |
| }); | |
| export async function getUrl(event: APIGatewayEvent) { | |
| const fileType = event.queryStringParameters?.filetype; | |
| if (!fileType?.startsWith("image")) { | |
| return { | |
| statusCode: 400, | |
| body: JSON.stringify({ error: "Please upload an image" }), | |
| }; | |
| } | |
| // image/png -> png | |
| const extension = fileType.split("/")[1]; | |
| const imageId = uuidv4(); | |
| const filename = `${imageId}.${extension}`; | |
| const expireSeconds = 60 * 5; | |
| const readUrl = `https://lovebox-stash.s3.amazonaws.com/${filename}`; | |
| const uploadUrl = s3.getSignedUrl("putObject", { | |
| Bucket: "lovebox-stash", | |
| Key: filename, | |
| ContentType: fileType, | |
| Expires: expireSeconds, | |
| }); | |
| const headers = { | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Credentials": true, | |
| "access-control-allow-methods": "GET", | |
| }; | |
| return { | |
| statusCode: 200, | |
| headers, | |
| body: JSON.stringify({ | |
| uploadUrl, | |
| readUrl, | |
| imageId, | |
| }), | |
| }; | |
| } |