简体   繁体   English

如何清除Firebase Cloud Functions中的临时文件

[英]How to clean temporary files in Firebase Cloud Functions

I'm trying to process two different images with the same name (and this is not exchangeable) with cloud functions. 我正在尝试使用云函数处理具有相同名称(并且这是不可交换的)的两个不同图像。 The function is triggered to process only a image and the same happend for the second image. 触发该功能以仅处理图像,第二个图像也发生相同的情况。 The problem is I'm not able to delete temporary files, so the second image cannot be saved because it has the same path and the same name. 问题是我无法删除临时文件,因此第二张图像无法保存,因为它具有相同的路径和名称。 I use fs.unlinkSync() to clear temp folder but this not works. 我使用fs.unlinkSync()清除临时文件夹,但这不起作用。 This is the code: 这是代码:

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);

  });

The last line is assigned to clear the working directory where temp files are stored but this not work (processing the second image, returns always the first image). 最后一行被分配以清除存储临时文件的工作目录,但是该目录不起作用(处理第二个映像,始终返回第一个映像)。 I tried even to fs.unlincSync() the sigle files but not works. 我什至尝试fs.unlincSync()文件,但不起作用。

fs.unlinkSync() only works on individual files. fs.unlinkSync()仅适用于单个文件。 It does not work on entire directories. 它不适用于整个目录。 You're calling it on a directory type of file, and that's not going to work. 您在文件的目录类型上调用它,但这将无法正常工作。

You have a lot of options for deleting an entire directory. 您有很多删除整个目录的选项。 This question lists some of your options: Remove directory which is not empty 这个问题列出了您的一些选项: 删除不为空的目录

Why not do fs.remove(out) instead of fs.unlinkSync(workingDir) ? 为什么不使用fs.remove(out)而不是fs.unlinkSync(workingDir)? I'm assuming you're using https://www.npmjs.com/package/fs-extra 我假设您正在使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM