We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 59b4e50 commit efd33aeCopy full SHA for efd33ae
4 files changed
backend/config.js
@@ -7,4 +7,6 @@ export default {
7
MONGODB_URL: process.env.MONGODB_URL || 'mongodb://localhost/amazona',
8
JWT_SECRET: process.env.JWT_SECRET || 'somethingsecret',
9
PAYPAL_CLIENT_ID: process.env.PAYPAL_CLIENT_ID || 'sb',
10
+ accessKeyId: process.env.accessKeyId || 'accessKeyId',
11
+ secretAccessKey: process.env.secretAccessKey || 'secretAccessKey',
12
};
backend/routes/uploadRoute.js
@@ -1,5 +1,8 @@
1
import express from 'express';
2
import multer from 'multer';
3
+import multerS3 from 'multer-s3';
4
+import aws from 'aws-sdk';
5
+import config from '../config';
6
const storage = multer.diskStorage({
destination(req, file, cb) {
@@ -17,4 +20,23 @@ const router = express.Router();
17
20
router.post('/', upload.single('image'), (req, res) => {
18
21
res.send(`/${req.file.path}`);
19
22
});
23
+
24
+aws.config.update({
25
+ accessKeyId: config.accessKeyId,
26
+ secretAccessKey: config.secretAccessKey,
27
+});
28
+const s3 = new aws.S3();
29
+const storageS3 = multerS3({
30
+ s3,
31
+ bucket: 'amazona-bucket',
32
+ acl: 'public-read',
33
+ contentType: multerS3.AUTO_CONTENT_TYPE,
34
+ key(req, file, cb) {
35
+ cb(null, file.originalname);
36
+ },
37
38
+const uploadS3 = multer({ storage: storageS3 });
39
+router.post('/s3', uploadS3.single('image'), (req, res) => {
40
+ res.send(req.file.location);
41
42
export default router;
frontend/src/screens/ProductsScreen.js
@@ -81,7 +81,7 @@ function ProductsScreen(props) {
81
bodyFormData.append('image', file);
82
setUploading(true);
83
axios
84
- .post('/api/uploads', bodyFormData, {
+ .post('/api/uploads/s3', bodyFormData, {
85
headers: {
86
'Content-Type': 'multipart/form-data',
87
},
package.json
@@ -4,13 +4,15 @@
"description": "Demo : https://amazonaapp.herokuapp.com/",
"main": "index.js",
"dependencies": {
+ "aws-sdk": "^2.702.0",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"eslint-plugin-react": "^7.19.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
13
"mongoose": "^5.8.11",
- "multer": "^1.4.2"
14
+ "multer": "^1.4.2",
15
+ "multer-s3": "^2.9.0"
16
"devDependencies": {
"@babel/cli": "^7.8.4",
0 commit comments