该功能在存储桶中的每次上传后执行。它会转换上传文件并将其上传到存储桶中,然后在firebase文档中更改“下载地址”。
每10秒钟上传一次,效果很好,但每10秒钟上传一次,效果却很奇怪。
它将最后上传中的网址写入数据库。这种行为是自觉的,但我找不到错误。
错误: 上传文件->压缩->将新文件网址保存在数据库中,一切正常
当我上传2个文件时,第一个文件工作正常。 第二个文件将被正确上传,新的压缩文件也将具有正确的名称,但对于所有后续的所有上传,第一个文件的内容将保持相同,直到一段时间。
export const smallermemes = functions.storage.bucket('memelyuserkawaiuploads')
.object()
.onFinalize(async (object: ObjectMetadata) => {
console.log('starting exe smallermemes');
if (object) {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name || "";
const filePathArray = filePath.split('/')
const fileName = filePathArray.pop() || "";
const fileDir = filePathArray[0] || "";
const bucketDir = dirname(filePath);
const workingDir = join(tmpdir(), 'compress');
const tmpFilePath = join(workingDir, 'source.png');
if (fileName.includes('compress@') || !object.contentType || !object.contentType.includes('image') || !fileDir.includes("uploads")) {
console.log('exiting function smallermemes');
return false;
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir);
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
});
// 3. Resize the images and define an array of upload promises
const thumbName = path.parse(`compress@_${fileName}`).name + '.webp';
const thumbPath = join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.webp({ quality: 75 })
.toFile(thumbPath);
// Upload to GCS
await bucket.upload(thumbPath, {
destination: join(bucketDir, thumbName)
});
const thumbFile = await bucket.file(join(bucketDir, thumbName));
const config = {
action: 'read',
expires: '03-01-2500',
};
const [thumbFileUrl] = await thumbFile.getSignedUrl(config);
await admin.firestore().collection('uploadsCollection').doc(fileName).update({ DownloadUrl: thumbFileUrl });
console.log("fileName: " + fileName);
console.log("thumbName: " + thumbName);
// 5. Cleanup remove the tmp/compress from the filesystem
return fs.remove(workingDir);
}
return false;
});
我猜错是在getSignedUrl调用中,但是我在关注文档 https://cloud.google.com/nodejs/docs/reference/storage/2.5.x/File#getSignedUrl
答案 0 :(得分:0)
发现Sharp具有某种内部缓存
https://github.com/lovell/sharp/issues/1848 https://sharp.pixelplumbing.com/en/stable/api-utility/#cache
禁用它可以解决我的问题