javascript - 将原始图像和带水印的图像上传到 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, 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);
});
};
解决方案
推荐阅读
- python - 是否有一种可移植的方式来提供在 PyPI 上分发的包的本地化?
- mysql - SQL 错误代码:1172 更新未插入
- java - 拆分字符串!在 JAVA 中
- spring - 在 Spring Boot 2.0.2 版本中注入特定数据源
- javascript - JS中数组的两个正确答案
- bash - 如何通过 SSH 使用 CURL 并获取文件以及返回值?
- vue.js - 使用 Vuex 设置 Vue.js
- angularjs - 当控制台日志给出好的结果时,如何显示与我的控制台日志相同的数据?
- c# - 使用 cookie 在 HttpWebRequest (WPF) 中自动设置国家
- bash - Bash 别名到备份文件夹到 tar.gz