I am trying to upload files from Express ejs to AWS s3 bucket I am successful in doing so but when I am trying to select files from other directory or folder other than where my index.js is it doesn't accept the file and throw error file not found.
index.js
'use strict';
const express = require('express');
const app = express();
const multer = require('multer');
const multerS3 = require('multer-s3');
const AWS = require('aws-sdk');
const bodyParser = require('body-parser');
const s3 = new AWS.S3({
accessKeyId: '',
secretAccessKey: ''
});
app.use(bodyParser.urlencoded({extended : true}));
app.set('view engine', 'ejs');
const uploadS3 = multer({
storage: multerS3({
s3: s3,
bucket: '',
metadata: (req, file, cb) => {
cb(null, {fieldName: file.fieldname})
},
key: (req, file, cb) => {
cb(null, Date.now().toString() + '-' + file.originalname)
}
})
});
test.ejs
<html>
<form method="post" action="/upload">
<input type="file" name="file" />
<input type="submit" />
</form>
</html>
route.js
var fileupload = require('../../common/service/file-upload');
//some code in between
app.post('/upload', fileupload.uploadS3.single('file'),(req, res) => {
console.log(req.file);
});
Man, how could Express read client's file by path? The files are uploaded / sent in the POST request.
Use some body parser that support multipart/form-data
file upload, like mutler , to parse the file from the request & upload it to S3.
In example...
html
<html>
<!-- don't forget enctype parameter -->
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
</html>
node server using AWS and multer manually
const multer = require('multer'),
AWS = require('aws-sdk'),
S3 = new AWS.S3({
...
});
router.post('/', multer().single('file'), (req, res) => {
// debug req.file
console.log(req.file);
S3.upload({
Bucket: '...',
Key: req.file.originalname,
Body: req.file.buffer
}, (err, res) => {
if (err) throw err;
res.json({
message: "File uploaded to S3"
});
});
});
node server using multer-s3
const multer = require('multer'),
multerS3 = require('multer-s3'),
AWS = require('aws-sdk'),
S3 = new AWS.S3({
...
});
let upload = multer({
storage: multerS3({
s3: s3,
bucket: '...',
key: (req, file, cb)
=> cb(null, file.originalname), // or whatever Key you like
})
});
router.post('/', upload.single('file'), (req, res) => {
res.json({
message: "File uploaded to S3"
});
});
whenever you are uploading the files you need to add attribute to form enctype="multipart/form-data". .then it will work. to upload files to s3 use multer-s3 package which is simple to configure Refrence: https://www.npmjs.com/package/multer-s3
so your test.ejs file will be like this
<html>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
<const/form>
</html>
and index.js
const express = require('express');
const bodyParser = require('body-parser');
const AWS = require('aws-sdk');
const multer = require('multer')
const multerS3 = require('multer-s3')
const app = express();
const s3 = new AWS.S3({
accessKeyId: //aws access key ,
secretAccessKey: //aws secret key
});
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'some-bucket',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
})
app.post('/', upload.single('file'), (req, res) => {
res.json({
message: "File uploaded to S3"
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.