所以我正在使用Google Storage API调整图像大小。代码如下所示。在触发器的第一次运行中,它会按预期调整图像的大小。
但是第二次触发该功能时,它会触发第一张图像,而不是当前上传的图像。
我不确定在这种情况下该怎么办。 onFinalize()引用是否不变?
export const generateThumbs = functions.storage.object().onFinalize(async object => {
const bucket = gcs.bucket(object.bucket)
const filePath = object.name
const fileName = filePath.split('/').pop()
const bucketDir = dirname(filePath)
console.log("fileName is " + fileName)
const workingDir = join(tmpdir(), 'thumbs')
console.log(("workingDir "))
const tmpFilePath = join(workingDir, 'source.png')
if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
console.log('exiting function');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
});
// 3 define the size
const sizes = 256
const uploadPromises = (async (size) => {
const thumbName = `thumb@${size}_${fileName}`;
const thumbPath = join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: join(bucketDir, thumbName)
});
})
// 4. Run the upload operations
await uploadPromises
// 5. Cleanup remove the tmp/thumbs from the filesystem
return fs.remove(workingDir)
})