Skip to content

Commit 7010ae4

Browse files
Implementation: S3 document upload
1 parent 0f58504 commit 7010ae4

File tree

6 files changed

+1738
-0
lines changed

6 files changed

+1738
-0
lines changed

configs/s3_config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { S3Client} = require("@aws-sdk/client-s3");
2+
require("dotenv").config();
3+
4+
const s3 = new S3Client({
5+
region: process.env.AWS_REGION , // Your AWS region
6+
credentials : {
7+
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Your AWS access key ID
8+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, // Your AWS secret access key
9+
}
10+
});
11+
12+
exports.s3 = s3; // Export the S3 client for use in other files
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const { GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3");
2+
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
3+
require("dotenv").config();
4+
const asyncHandler = require("../middlewares/asyncHandler");
5+
const { s3 } = require("../configs/s3_config"); // Import the S3 client from config.js
6+
7+
const Bucket = process.env.AWS_BUCKET_NAME; // Your S3 bucket name
8+
//const Key ='farmpond4.pdf';
9+
const Expires = 90; // URL valid for 60 seconds
10+
const ContentType = 'application/pdf'; // Set the content type to PDF
11+
12+
//get-upload url
13+
exports.getUploadUrl = asyncHandler(async (req, res) => {
14+
const Key = req.query.fileName;
15+
if (!Key) {
16+
return res.status(400).json({ error: "Missing 'fileName' in query params" });
17+
}
18+
19+
console.log(Key);
20+
21+
22+
const command = new PutObjectCommand({Bucket, Key, ContentType});
23+
try {
24+
const uploadURL = await getSignedUrl(s3,command, {Expires});
25+
//console.log('Upload URL (Server):', uploadURL);
26+
//return uploadURL;
27+
res.json(uploadURL);
28+
29+
} catch (error) {
30+
console.error('Error generating presigned URL', error);
31+
//res.status(500).send('Error generating presigned URL');
32+
throw error;
33+
}
34+
});
35+
36+
//get-download url
37+
exports.getDownloadUrl = asyncHandler( async (req, res) => {
38+
const Key = req.query.fileName;
39+
const command = new GetObjectCommand({ Bucket, Key });
40+
try {
41+
const downloadURL = await getSignedUrl(s3, command, { Expires });
42+
//console.log('Download URL (Server):', downloadURL);
43+
res.json(downloadURL);
44+
} catch (error) {
45+
console.error('Error generating presigned URL', error);
46+
//res.status(500).send('Error generating presigned URL');
47+
throw error;
48+
}
49+
});

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ app.use('/api/dashboard', dashboardRoutes);
1919
const formDataRoutes = require('./routes/formData.route');
2020
app.use('/api/formData', formDataRoutes);
2121

22+
const fileRoutes = require('./routes/files.route');
23+
app.use('/api/files',fileRoutes);
24+
2225
app.get("/", (req, res) => {
2326
res.send("✅ Backend is running and ready!");
2427
});

0 commit comments

Comments
 (0)