![](/img/trans.png)
[英]How to read cert files from Cloud Functions for Firebase Folder
[英]How to clean temporary files in Firebase Cloud Functions
我正在尝试使用云函数处理具有相同名称(并且这是不可交换的)的两个不同图像。 触发该功能以仅处理图像,第二个图像也发生相同的情况。 问题是我无法删除临时文件,因此第二张图像无法保存,因为它具有相同的路径和名称。 我使用fs.unlinkSync()
清除临时文件夹,但这不起作用。 这是代码:
exports.thumb= functions.database.ref('/{uid}/upload')
.onUpdate(async (change, context) => {
const fileName = "RawImage.jpg";
const userId = context.auth.uid;
const bucket = storage.bucket("-----");
const workingDir = path.join(os.tmpdir(), "thumbs");
const tempFilePath = path.join(workingDir, fileName);
const filePath = path.join(userId,fileName);
await fs.ensureDir(workingDir);
await bucket.file(filePath).download({destination: tempFilePath});
const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(userId, thumbFileName);
const out = path.join(workingDir, thumbFileName);
const uploadPromises = async () => {
await sharp(tempFilePath)
.resize(300, 200)
.grayscale()
.toFile(out);
return await bucket.upload(out, {
destination: thumbFilePath,
});
}
const v = await uploadPromises();
return fs.unlinkSync(workingDir);
});
最后一行被分配以清除存储临时文件的工作目录,但是该目录不起作用(处理第二个映像,始终返回第一个映像)。 我什至尝试fs.unlincSync()
文件,但不起作用。
fs.unlinkSync()
仅适用于单个文件。 它不适用于整个目录。 您在文件的目录类型上调用它,但这将无法正常工作。
您有很多删除整个目录的选项。 这个问题列出了您的一些选项: 删除不为空的目录
为什么不使用fs.remove(out)而不是fs.unlinkSync(workingDir)? 我假设您正在使用https://www.npmjs.com/package/fs-extra
onUpdateCallback(change, context) {
... // The other code
const out = path.join(workingDir, thumbFileName);
const uploadPromises = async () => {
await sharp(tempFilePath).resize(300, 200).grayscale().toFile(out);
return await bucket.upload(out, {destination: thumbFilePath});
}
const v = await uploadPromises();
// return fs.unlinkSync(workingDir);
return fs.remove(out); // Why not this instead?
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.