我正在使用 multer 、aws-sdk 和 multer-s3 软件包以及 Express。
当用户编辑个人资料时,用户可以更改或不更改个人资料图片/头像。
我已经传递了 multer 对象
multer({storage: multer.memoryStorage()}).single('profileHeroImageEdit')
如果文件包含当前请求,那么我会将文件上传到 s3 存储桶,但我没有收到 upload_replace 的任何响应,其中 req.file.location 将提供S3 存储桶的 url(文件位置)。 在 upload_replace 中,我可以获得我尝试上传的文件(req.file),但我想要上传文件到 S3 存储桶的位置。
我缺少什么?帮助将不胜感激
router.put("/:id",multer({ storage:
multer.memoryStorage()}).single('profileHeroImageEdit'),
middleware.checkProfileOwnership,function(req, res){
if(isFileExists(req)==false){
delete req.body.profileHeroImage
}
else{
console.log('file has')
var upload_replace = multer({
limits:{
fileSize:MAX_FILE_SIZE,
files:1
},
storage: multerS3({
s3: photoBucket,
bucket: BucketName,
acl: 'public-read',
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
cb(null,Date.now().toString())
}
})
}).single('profileHeroImageEdit')
upload_replace(req, res, function (err,log) {
console.log('request log')
console.log(req.file.location)
console.log()
});
}
Profile.findByIdAndUpdate(req.params.id, req.body.profile, function(err, updatedProfile){
if (err){
res.redirect("/profiles");
} else {
res.redirect("/profiles/" + req.params.id);
}
});
});
function isFileExists(request){
if(request.file)
{
return true
}
else{
return false
}
}
最佳答案
我有使用 multer 和 aws-sdk 的完整代码
包含此文件并 npm install all
//aws s3 packages const aws = require("aws-sdk"); const multerS3 = require("multer-s3"); const multer = require("multer"); const path = require("path");
然后
//profile image upload start const s3 = new aws.S3({ accessKeyId: "***", secretAccessKey: "***", Bucket: "***" }); //Singe profile image upload const profileImgUpload = multer({ storage: multerS3({ s3: s3, bucket: "***", acl: "public-read", key: function(req, file, cb) { cb( null, path.basename(file.originalname, path.extname(file.originalname)) + "-" + Date.now() + path.extname(file.originalname) ); } }), limits: { fileSize: 2000000 }, // In bytes: 2000000 bytes = 2 MB fileFilter: function(req, file, cb) { checkFileType(file, cb); } }).single("profileImage"); // getExtension of file by this function function getExtension(filename) { var parts = filename.split("."); return parts[parts.length - 1]; } //checkfile type of input function function checkFileType(file, cb) { const ext = getExtension(file.originalname); switch (ext.toLowerCase()) { case "jpeg": case "jpg": case "png": case "gif": return cb(null, true); } cb("Error: Images Only!"); } router.post( "/image", passport.authenticate("jwt", { session: false }), (req, res) => { profileImgUpload(req, res, error => { if (error) { res.json({ error: error }); } else { //here we can get req.body const userDp = {}; //end of getting values // If File not found then dont store anything if (req.file !== undefined) userDp.dpUrl = req.file.location; // Save the file name into database into profile model User.findOne({ email: req.user.email }).then(user => { if (user) { // Update User.findOneAndUpdate( { email: req.user.email }, { $set: userDp }, { new: true } ).then(user => res.json(user)); } else { res.json({ msg: "not able not update data" }); } }); } }); } );
3.需要使用react前端发送数据
const data = new Formdata();
data.append()
并且还包含标题
关于node.js - Amazon S3 存储桶文件上传响应不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49696866/