gpt4 book ai didi

node.js - 使用nodejs lambda的S3文件上传问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:17:41 25 4
gpt4 key购买 nike

我尝试使用nodejs lambda函数将图像上传到aws s3存储桶。但对于初始调用,没有文件被上传,并且当下次尝试时,先前的文件被上传。

即使我们在异步等待中使用,它也不能同步工作。

async uploadAttachment(attachment, id) {
try {
let res = '';
attachment.forEach(async (element, index) => {
const encodedImage = element.base64;
const fileTypeInfo = element.fileextType;
const fileName = `${Math.floor(new Date() / 1000)}_${index + 1}.${fileTypeInfo}`;
const decodedImage = Buffer.from(encodedImage, 'base64');
const filePath = `${id}/${fileName}`;
const params = {
Body: decodedImage,
Bucket: process.env.S3_FRF_BUCKET,
Key: filePath
};
res = await s3.upload(params, () => {});
});
return res;
} catch (e) {
throw e;
}
}

有什么建议吗?

最佳答案

常规数组上的

.forEach 不能像人们期望的那样与 async/await 一起使用。使用 for..of 代替。

for(let element of attachment) {
// await actually waits here
}

关于node.js - 使用nodejs lambda的S3文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54147977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com