-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_upload.js
34 lines (30 loc) · 973 Bytes
/
image_upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const express = require('express');
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const expapp = express();
const s3Bucket = new aws.S3({
accessKeyId: 'AKIARA2XBS3AN5CMRMHB',
secretAccessKey: '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',
region: 'US East (Ohio) us-east-2'
});
const upload = multer({
storage: multerS3({
s3: s3Bucket,
bucket: 'masspirgphotoupload',
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function (req, file, cb) {
if (!file.originalname.match(/\.(jpg|jpeg|png|HEIC)$/)) {
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, Date.now().toString())
}
})
});
expapp.post('/upload', upload.single('image'), function (req, res) {
res.json({ success: true, message: 'Image uploaded successfully.' });
});
expapp.listen(3000, function () {
console.log('Server started on port 3000.');
});