I am utilizing a package called multer-s3-transform
to modify the incoming image before uploading it to my bucket. Below is the code snippet of how I am implementing this:
const singleImageUploadJpg = multer({
storage: multerS3({
s3: s3,
bucket: "muh-bucket",
acl: "public-read",
key: function(req, file, cb) {
const fileName = uuid.v4();
cb(null, fileName);
},
shouldTransform: function(req, file, cb) {
cb(null, true);
},
transforms: [
{
id: "original",
key: function(req, file, cb) {
cb(null, `${uuid.v4()}.jpg`);
},
transform: function(req, file, cb) {
cb(
null,
sharp()
.resize()
.jpeg({ quality: 50 })
);
}
},
{
id: "small",
key: function(req, file, cb) {
cb(null, `${uuid.v4()}_small.jpg`);
},
transform: function(req, file, cb) {
cb(
null,
sharp()
.resize()
.jpeg({ quality: 50 })
);
}
}
]
}),
limits: { fileSize: 50 * 1024 * 1024 }
}).single("image");
A concern I have encountered is that the uuid generated will always differ between the small and original versions. How can I pass down the value of const fileName = uuid.v4()
to each callback so that both versions have the same name except for the _small
appended to one version?