Поэтому я использую API хранилища Google для изменения размера изображений. Код показан ниже. При первом запуске триггера он изменил размер изображения, как и ожидалось.
Но когда функция запускается во второй раз, она запускает первое изображение, а не загруженное в данный момент.
Я не уверен, что делать в этом случае. Ссылка 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)
})