From c223875eeca3c2811e608da3d82e97323da01c74 Mon Sep 17 00:00:00 2001 From: Sophia Hooley <143217945+Sophiahooley@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:46:19 -0600 Subject: [PATCH] maybe finish s3 backend but tbd testing incoming --- api/src/config/aws.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 api/src/config/aws.ts diff --git a/api/src/config/aws.ts b/api/src/config/aws.ts new file mode 100644 index 0000000..02256ae --- /dev/null +++ b/api/src/config/aws.ts @@ -0,0 +1,43 @@ +require('dotenv').config(); +const AWS = require('aws-sdk'); +const express = require('express'); +const app = express(); + +// Configure AWS SDK +const s3 = new AWS.S3({ + accessKeyId: process.env.AWS_ACCESS_KEY_ID, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + region: process.env.AWS_REGION, +}); + +// Route to list all files in the S3 bucket +app.get('/files', (req, res) => { + const params = { + Bucket: process.env.S3_BUCKET_NAME, + }; + + s3.listObjectsV2(params, (error, data) => { + if (error) { + return res.status(500).send(error); + } + + // Map each file to its URL and return the list + const fileUrls = data.Contents.map((file) => { + return { + key: file.Key, + url: s3.getSignedUrl('getObject', { + Bucket: process.env.S3_BUCKET_NAME, + Key: file.Key, + Expires: 60 * 60, // Link expires in 1 hour + }), + }; + }); + + res.status(200).json(fileUrls); + }); +}); + +// Start server +app.listen(3001, () => { + console.log('Server started on port 3001'); +});