-
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.
Add ability to store images to s3 bucket in backend
- Loading branch information
Showing
7 changed files
with
1,900 additions
and
285 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,32 @@ | ||
import AWS from "aws-sdk"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
// 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, // Ensure you use the right region | ||
}); | ||
|
||
export const uploadToS3 = async (file: Express.Multer.File) => { | ||
const params = { | ||
Bucket: process.env.AWS_BUCKET_NAME as string, // S3 bucket name | ||
Key: `${Date.now()}_${file.originalname}`, // Unique file name | ||
Body: file.buffer, // File buffer from multer's memoryStorage | ||
ContentType: file.mimetype, | ||
}; | ||
|
||
const data = await s3.upload(params).promise(); | ||
return data.Location; // Returns the file URL after upload | ||
}; | ||
|
||
export const deleteFromS3 = async (key: string) => { | ||
const params = { | ||
Bucket: process.env.AWS_BUCKET_NAME as string, | ||
Key: key, | ||
}; | ||
|
||
await s3.deleteObject(params).promise(); | ||
}; |
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,45 @@ | ||
import multer from "multer"; | ||
import path from "path"; | ||
|
||
// Define storage settings | ||
const storage = multer.memoryStorage(); // Store files in memory to directly upload to S3 | ||
|
||
const upload = multer({ | ||
storage, | ||
fileFilter: (req, file, cb) => { | ||
const fileTypes = /jpeg|jpg|png/; | ||
const extname = fileTypes.test( | ||
path.extname(file.originalname).toLowerCase() | ||
); | ||
const mimetype = fileTypes.test(file.mimetype); | ||
if (extname && mimetype) { | ||
return cb(null, true); | ||
} else { | ||
cb(new Error("Only images are allowed")); | ||
} | ||
}, | ||
}); | ||
|
||
export default upload; | ||
|
||
// import multer from "multer"; | ||
// import multerS3 from "multer-s3"; | ||
// import s3 from "../config/s3"; | ||
|
||
// // Set up the multer middleware with S3 storage | ||
// const upload = multer({ | ||
// storage: multerS3({ | ||
// s3: s3, | ||
// bucket: process.env.S3_BUCKET_NAME, | ||
// acl: "public-read", // make the files publicly readable (optional) | ||
// metadata: (req, file, cb) => { | ||
// cb(null, { fieldName: file.fieldname }); | ||
// }, | ||
// key: (req, file, cb) => { | ||
// cb(null, `questions/${Date.now().toString()}-${file.originalname}`); | ||
// }, | ||
// }), | ||
// limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB limit | ||
// }); | ||
|
||
// export default upload; |
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