【发布时间】:2017-12-04 18:08:25
【问题描述】:
function uploadGuideMedia(req, res, next) {
if (!req.file && !req.body.id) {
return res.status(401).send({
status: 401,
data: "Upload a file with 'file' key"
});
}
db.query('SELECT * from guide_media where _id = $1', [req.body.id], function(err, guide) {
if (err) return next(err);
if (guide.rows.length == 0) {
return res.status(404).send({
status: 404,
data: "Guide_media not found"
})
} else {
let name = req.file.path + path.extname(req.file.originalname);
fs.renameSync(req.file.path, name);
req.file.path = name;
var s3bucket = new AWS.S3({
params: {
Bucket: 'mapery-v2'
}
});
fs.readFile(req.file.path, function(err, lq_file) {
s3bucket.createBucket(function() {
var params = {
Key: 'upload-v2/' + req.file.originalname,
Body: lq_file,
ContentType: req.file.mimetype,
ACL: 'public-read'
};
s3bucket.upload(params, function(err, aws_images) {
fs.unlink(req.file.path, function(err) {
db.query('UPDATE guide_media SET image_path = $1 WHERE _id = $2 RETURNING *', [aws_images.Location, req.body.id], function(err, guide_res) {
if (err) return next(err);
return res.status(200).send({
status: 'success',
data: guide_res.rows
});
});
});
})
})
})
}
});
}
【问题讨论】:
-
为了稳定性和速度,AWS 默认使用分段上传,如果您不指定默认最大上传数,则它需要 5mb 块。如果你想在 1go 中上传,我相信你需要将 max-uploads 设置为 1。
-
感谢您的回复。我需要在
params变量中指定??
标签: node.js amazon-web-services file-upload postman