我已经将我的节点js web上的图像上传到S3,使用以下代码:
router.post('/uploadtest', multipartMiddleware, function(req, res, next) {
var file = req.files.file;
var data = {Key: file.originalFilename, Body: file.path, ContentType: file.type, ACL: 'public-read'};
s3Bucket.putObject(data, function(err, data) {
if (err)
{ console.log('Failed!');
res.redirect("/lookpic?url=null");
} else {
console.log('Success!');
var urls = 'https://'+sendurl+'.amazonaws.com/'+myBucket+'/'+file.originalFilename
res.redirect("/lookpic?url="+urls);
}
});
});
它的工作,我的图像存储到我的S3存储桶,但当我点击链接看到我的图像,这是结果: http://i.stack.imgur.com/qAM5N.png
我的步骤出了什么问题?
答案 0 :(得分:0)
您只是在body属性中指定文件路径,而文件数据应该在body属性中提供。试试这个:
var fs = require('fs');
router.post('/uploadtest', multipartMiddleware, function(req, res, next) {
var file = req.files.file;
var file_data = fs.readFileSynch(file.path);
var data = {Key: file.originalFilename, Body: file_data, ContentType: file.type, ACL: 'public-read'};
s3Bucket.putObject(data, function(err, data) {
if (err)
{ console.log('Failed!');
res.redirect("/lookpic?url=null");
} else {
console.log('Success!');
// remove the uploaded file if you want to
fs.unlink(file.path, function(err) {
if (err) throw err;
});
//
var urls = 'https://'+sendurl+'.amazonaws.com/'+myBucket+'/'+file.originalFilename
res.redirect("/lookpic?url="+urls);
}
});
});
使用fs模块同步读取要上载的文件。查看此链接:https://nodejs.org/api/fs.html