如何解决将原始图像和水印图像上传到 aws s3 存储桶的问题
我正在为我的项目开发功能,将输入的图像上传到 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 举报,一经查实,本站将立刻删除。