-
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.
maybe finish s3 backend but tbd testing incoming
- Loading branch information
1 parent
2b91e9d
commit c223875
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); |