问题描述
我正在为我的项目开发功能,将输入的图像上传到 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,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);
});
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)