const AWS = require('aws-sdk'); const express = require('express'); const fs = require('fs'); const multer = require('multer'); const path = require('path'); require('dotenv').config() const app = express(); const port = 3000; // Configure AWS AWS.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION, }); const s3 = new AWS.S3(); const upload = multer({ dest: 'uploads/' }); // POST endpoint to upload an image app.post('/image', upload.single('image'), (req, res) => { const file = req.file; // Read the file from the temporary location fs.readFile(file.path, (err, data) => { if (err) throw err; // Handle error const params = { Bucket: 'gifts-image', // S3 Bucket name Key: `${Date.now()}_${path.basename(file.originalname)}`, // File name to use in S3 Body: data, //ACL: 'public-read' // Optional: if you want the uploaded file to be publicly accessible }; // Upload the file to S3 s3.upload(params, function(s3Err, data) { if (s3Err) throw s3Err; // Handle upload error // Once successfully uploaded to S3, send back the URL of the uploaded file res.send(`File uploaded successfully at ${data.Location}`); }); }); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });