我正在为我的项目开发功能,将输入的图像上传到 aws,使用 jimp 为其添加水印,然后将带水印的图像上传到 aws。所有功能都有效,但是,只有预加水印的图像被上传两次,而加水印的图像似乎在上传到 aws 后更新。我相信它与异步有关并且使用了 Promise,但由于我是 js 新手,我不太确定我是否遗漏了一些明显的东西。我在下面附上了我的三个功能。
const uploadFile = async (fileName) => {
//Read content from file
const fileContent = fs.readFileSync(fileName);
//setting up S3 upload parameters
const params = {
Bucket: BUCKET_NAME,
Key: 'original_test.jpg', //filename to save as in S3
Body: fileContent,
};
//uploading image to the bucket
s3.upload(params)
.promise()
.then((data) => {
console.log('Success');
watermarker(fileName);
})
.catch(function(err) {
console.log(err);
});
};
const watermarker = async (uploadedImage) => {
const promises = function jimpify() {
return Jimp.read(uploadedImage)
.then(function(image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_128_BLACK);
})
.then(function(font) {
loadedImage.print(font, 10, 10, imageCaption).write(uploadedImage);
})
.catch(function(err) {
console.error(err);
});
Promise.all(promises).then(() => watermarkImageUploader(uploadedImage));
};
const watermarkImageUploader = async (watermarkedImage) => {
//Read content from watermark image
const watermarkFileContent = fs.readFileSync(watermarkedImage);
//watermark image S3 upload parameter
const watermarkParams = {
Bucket: BUCKET_NAME,
Key: 'watermark_test.jpg', //filename to save as in S3
Body: watermarkFileContent,
};
//uploading watermark image to the bucket
s3.upload(watermarkParams)
.promise()
.then(function(data) {
console.log('Success 2');
})
.catch(function(err) {
console.log(err);
});
};