帮助!没关系,我似乎无法使用multer-s3为图像提供自定义文件名。我有这个非常酷的自定义功能,可以将图像上传到s3。如果我使用文件的原始文件名,则效果很好,但是当我尝试使用自定义文件名时,它将在新文件名下上载第一张图片3次。如果有人有任何建议,见解或知道为什么不起作用。我会同意的。
var AWS = require("../AWS").AWS;
var s3 = require("../AWS").s3;
var multer = require("multer");
var multerS3 = require("multer-s3");
function generateKey(file, newFileName) {
// var finalFileName = newFileName + "." + file.originalname.split(".")[1];
var finalFileName = newFileName + "." + file.originalname.split(".")[1];
return finalFileName;
}
async function singleFileUpload(req, res, newFileName, bucketName, fieldName) {
var fileFilter = (req, file, cb) => {
var ext = file.originalname.split(".").slice(-1);
if (ext == "jpg" || ext == "mp4" || ext == "wmv") {
cb(null, true);
} else {
cb(new Error("invalid file format"), false);
}
};
var upload = multer({
fileFilter,
storage: multerS3({
s3,
bucket: bucketName,
acl: "public-read",
metadata: function(req, file, cb) {
cb(null, { test: "testing_meta_data!" });
},
// key: function(req, file, cb) {
// // let fileExtension = file.originalname.split(".")[1];
// let finalFileName = file.originalname;
// console.log(finalFileName);
// cb(null, finalFileName);
// }
key: function(req, file, cb) {
var newKey = generateKey(file, newFileName);
console.log("newKey", newKey);
cb(null, newKey);
}
})
});
console.log(fieldName);
var singleUpload = upload.any(fieldName);
await singleUpload(req, res, error => {
if (error) {
throw error;
} else {
console.log("it worked");
}
});
}
比起我这样调用自定义函数
singleFileUpload(req, res, "myNewFileName" "mybucketName", fieldName);
我多次调用此命令是因为我希望能够重命名每个文件。 谢谢。
答案 0 :(得分:1)
此代码有效。
var upload = multer({
storage: multerS3({
s3,
bucket: process.env.BUCKET_NAME,
metadata: function(req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function(req, file, cb) {
cb(null, reNameFiles(file, req.body.id, req.originalUrl));
},
contentType: multerS3.AUTO_CONTENT_TYPE
})
});
function reNameFiles(file, id, type) {
let lastIndex = type.lastIndexOf("/");
let videoType = type.slice(lastIndex + 1);
let finalFileName = "";
if (videoType == "templateMainVideoThumbnail") {
finalFileName =
"templates/" +
id +
"/main/original/" +
"video" +
"." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
for (let i = 1; i <= 4; i++) {
if (videoType === "templateExampleVideo" + i) {
finalFileName =
"templates/" +
id +
"/example" +
i.toString() +
"/original/example" +
i.toString() +
"." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
}
for (let j = 1; j <= 4; j++) {
if (videoType === "templateExamplePoster" + j) {
finalFileName =
"templates/" +
id +
"/example" +
j.toString() +
"/poster." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
}
if (videoType === "templatePosterImage") {
finalFileName =
"templates/" +
id +
"/poster/poster" +
"." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
if (videoType == "templateThumbnailFiles") {
let cleanOrginalName = file.originalname.replace(/\s/g, "");
finalFileName =
"templates/" + id + "/thumbnails/" + Date.now() + cleanOrginalName;
return finalFileName;
}
if (videoType == "stockMainVideoThumbnail") {
finalFileName =
"stock/" +
id +
"/main/original/" +
"video" +
"." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
if (videoType == "stockPosterImage") {
finalFileName =
"stock/" +
id +
"/poster/poster" +
"." +
file.originalname.split(".").slice(-1);
return finalFileName;
}
if (videoType == "stockDownloadableFiles") {
finalFileName = "stock/" + id + "/downloadable/" + file.originalname;
return finalFileName;
}
}
答案 1 :(得分:0)
嗨,我正在使用这种方法通过s3-multer重命名和上传我的文件,您可以使用相同的方法:
`key: function (req, file, cb) {
file.originalname = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+path.extname(file.originalname);
var fullPath = 'public/signature/'+ file.originalname;
cb(null, fullPath)
}`