Amazon web services lambda在完成之前退出,不等待
我正在尝试创建一个lambda函数,该函数使用Amazon web services lambda在完成之前退出,不等待,amazon-web-services,aws-lambda,Amazon Web Services,Aws Lambda,我正在尝试创建一个lambda函数,该函数使用sharp调整图像大小,但是它在完成任务之前退出。我用了waitbe似乎它不等待 这是我的代码: const fileName = decodeURIComponent(event.Records[0].s3.object.key).replace(/\+/g, ' ') const bucket = event.Records[0].s3.bucket.name const keyName = fileName.split
sharp
调整图像大小,但是它在完成任务之前退出。我用了wait
be似乎它不等待
这是我的代码:
const fileName = decodeURIComponent(event.Records[0].s3.object.key).replace(/\+/g, ' ')
const bucket = event.Records[0].s3.bucket.name
const keyName = fileName.split(".").slice(0, -1).join(".")
const imageName = keyName.split("/").slice(-1).join()
const imageFormat = fileName.split(".").slice(-1).pop()
console.log("Getting object from S3")
const imageData = await s3.getObject({ Bucket: bucket, Key: fileName }).promise()
const imagePath = `/tmp/${imageName}.${imageFormat}`
console.log('creating image file')
fs.createWriteStream(imagePath)
console.log("Writing buffer on the file")
fs.writeFile(imagePath, imageData.Body, async () => {
const myMap = new Map();
await Promise.all(
sizes.map(async (size) => {
const newImageName = `${imageName}_${size.name}.${imageFormat}`
const output = `/tmp/${newImageName}`
console.log('Going to resize image')
await sharp(imagePath).resize({ width: size.width, height: size.height }).toFile(output)
console.log("Resized image")
myMap.set(size.name, { newImagePath: output, newImageName: newImageName })
})
)
console.log("map", myMap)
await Promise.all(
Array.from(myMap).map(async ([key, value]) => {
const params = {
Bucket: bucket,
Key: `images/${key}/${value.newImageName}`,
Body: value.newImagePath,
ContentType: `image/${imageFormat}`
}
console.log("Uploading to S3")
await s3.putObject(params).promise()
})
)
})
这是AWS cloudwatch日志:
START RequestId: 9199e286-c850-40ef-bc6e-e6541741bda7 Version: $LATEST
2021-04-26T05:52:45.150Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO Env variable is detected: true
2021-04-26T05:52:45.150Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO image format is: jpg
2021-04-26T05:52:45.150Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO Getting object from S3
2021-04-26T05:52:45.328Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO creating image file
2021-04-26T05:52:45.328Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO Writing buffer on the file
2021-04-26T05:52:45.348Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO Going to resize image
2021-04-26T05:52:45.370Z 9199e286-c850-40ef-bc6e-e6541741bda7 INFO Going to resize image
END RequestId: 9199e286-c850-40ef-bc6e-e6541741bda7
REPORT RequestId: 9199e286-c850-40ef-bc6e-e6541741bda7 Duration: 549.10 ms Billed Duration: 550 ms Memory Size: 512 MB Max Memory Used: 134 MB Init Duration: 760.73 ms
有什么想法吗?fs.writeFile会返回一个您忽略的承诺。您需要返回该承诺,以便Lambda知道等待,对吗?当您提供回调时,它不会返回承诺。这是一个问题,因为它调用的是异步回调,而不是等待对吗?