-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attachments infrastructure): Bucket, file scanning, and presigne…
…d urls. (#176) * uploads serfvice and presigned url generation * add missing export
- Loading branch information
Showing
14 changed files
with
903 additions
and
29 deletions.
There are no files selected for viewing
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,52 @@ | ||
import { response } from "../libs/handler"; | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
checkEnvVariables(["attachmentsBucketName", "attachmentsBucketRegion"]); | ||
|
||
const s3 = new S3Client({ | ||
region: process.env.attachmentsBucketRegion, | ||
}); | ||
|
||
export const handler = async (event: APIGatewayEvent) => { | ||
try { | ||
const body = JSON.parse(event.body); | ||
const bucket = process.env.attachmentsBucketName; | ||
const key = uuidv4(); | ||
const url = await getSignedUrl( | ||
s3, | ||
new PutObjectCommand({ | ||
Bucket: bucket, | ||
Key: key, | ||
}), | ||
{ | ||
expiresIn: 60, | ||
} | ||
); | ||
|
||
return response<unknown>({ | ||
statusCode: 200, | ||
body: { url, bucket, key }, | ||
}); | ||
} catch (error) { | ||
console.error({ error }); | ||
return response({ | ||
statusCode: 500, | ||
body: { message: "Internal server error" }, | ||
}); | ||
} | ||
}; | ||
|
||
function checkEnvVariables(requiredEnvVariables) { | ||
const missingVariables = requiredEnvVariables.filter( | ||
(envVar) => !process.env[envVar] | ||
); | ||
|
||
if (missingVariables.length > 0) { | ||
throw new Error( | ||
`Missing required environment variables: ${missingVariables.join(", ")}` | ||
); | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"name": "uploads", | ||
"description": "", | ||
"private": true, | ||
"version": "0.0.0", | ||
"main": "index.js", | ||
"scripts": {}, | ||
"author": "", | ||
"license": "CC0-1.0", | ||
"devDependencies": {} | ||
} |
Oops, something went wrong.