Javascript Node.js上传到Amazon S3正常,但文件损坏
我通过我的CMS提交了一份表格,其中包含一个图像和一些文本的文件选择器。代码运行&在我的S3帐户中创建了一个名称正确但已损坏的对象。例如,我正在上传JPG图像,但当我在s3仪表板中查看它们时,我只看到一个黑屏 非常感谢您的帮助 我的HTML表单:Javascript Node.js上传到Amazon S3正常,但文件损坏,javascript,node.js,amazon-s3,Javascript,Node.js,Amazon S3,我通过我的CMS提交了一份表格,其中包含一个图像和一些文本的文件选择器。代码运行&在我的S3帐户中创建了一个名称正确但已损坏的对象。例如,我正在上传JPG图像,但当我在s3仪表板中查看它们时,我只看到一个黑屏 非常感谢您的帮助 我的HTML表单: 更新 我相信您需要传递流而不是文件路径,您可以像这样使用fs.createReadStream: router.post('/updateschedule', isLoggedIn, upload.single('schedulepicture'),
更新
我相信您需要传递流而不是文件路径,您可以像这样使用fs.createReadStream:
router.post('/updateschedule', isLoggedIn, upload.single('schedulepicture'), function(req, res) {
var scheduleImageToUpload;
//Check if image was uploaded with the form & process it
if (typeof req.file !== "undefined") {
//Create Amazon S3 specific object
var s3 = new aws.S3();
var stream = fs.createReadStream(req.file.path)
//This uploads the file but the file cannot be viewed.
var params = {
Bucket: S3_BUCKET,
Key: req.file.originalname, //This is what S3 will use to store the data uploaded.
Body: stream, //the actual *file* being uploaded
ContentType: req.file.mimetype, //type of file being uploaded
ACL: 'public-read', //Set permissions so everyone can see the image
processData: false,
accessKeyId: S3_accessKeyId,
secretAccessKey: S3_secretAccessKey
}
s3.upload( params, function(err, data) {
if (err) {
console.log("err is " + err);
}
res.redirect('../adminschedule');
});
}
});