删除Google Cloud Functions的tmp文件夹中的文件 [英] Deleting Files in tmp folder of Google Cloud Functions

查看:69
本文介绍了删除Google Cloud Functions的tmp文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在os.tmpdir文件夹中的Google Cloud函数中写入文件.现在,我知道每次函数触发都略有不同,两次函数调用之间的tmp文件夹将永远不会相同(无法两次引用同一个tmp文件夹).我的问题是函数完成执行之前是否需要删除文件,或者是否自动取消了所有tmp内存的引用.

I'm currently writing into files in a google cloud functions in my os.tmpdir folder. Now I know that each time the functions fires is slightly different and the tmp folder will never be the same between function calls (can't reference the same tmp folder twice). My question is whether or not I need to delete the file before the function finishes execution or if all the tmp memory is dereferenced automatically.

当我写os.tmpdir时,当我查看目录时可以看到该文件,但是在随后的调用中却什么也看不到.我认为这意味着该文件以某种方式不再存在(不太可能),或者我正在其他位置查找.

edit: When I write to os.tmpdir I can see the file when I look into the directory but on subsequent calls I see nothing. I assume this means the file somehow no longer exists (unlikely) or I am looking in a different place.

下面是我用来编写文件的代码.

below is the code I user to write the file.

var wstream = fs.createWriteStream(os.tmpdir() + '/myOutput.txt');


wstream.write('Hello world!\n');
wstream.write('Another line\n');

wstream.end();      

wstream.on('finish', function () {
        console.log('file has been written');
        fs.readdir(os.tmpdir(), (err, files) => {
            console.log(files.length);
            files.forEach(file => {
                        console.log("Hey Again3", file);
                });
            })

edit 2:我一直在进行一些测试,而一个新系统的发生比这个问题所指示的发生的频率要高得多(我每次都观察到,而在这里我被告知几乎永远不会发生这种情况)).我还问了另一个问题,并从另一位Firebase员工那里收到了一些相互矛盾的建议,该建议与此处的个人测试更加一致:

edit 2: I've been running a few tests and the occurrence of a new system happens far more often than indicated in this question (I've observed every single time vs what I was told here that it would happen almost never). I also asked another question and received slightly conflicting advise from a different firebase employee that was more inline with the personal tests here: Can't Find file after having written to it in Google Cloud Functions

建议保持一致,但是我一次也没有观察到,在一次调用多个函数之间具有完全相同的文件系统,如下所示.

The advise stays consistent but I not once observed have the exact same file system between multiple calls of a single function as indicated below.

推荐答案

首先,由 os.tmpdir()标识的tmp文件夹始终相同.如果您发现不同的地方,请准确说明您所看到的.

Firstly, the tmp folder identified by os.tmpdir() is always the same. If you're observing something different, please clarify exactly what you're seeing.

第二,您没有义务从tmpdir中删除文件,但是它们会占用内存(这是基于内存的文件系统).保留这些文件会使您将来的函数调用变得更加昂贵,因为随着时间的流逝您要为内存付费.同样,您可能会在累积临时文件时冒用尽内存的风险.为了提高效率,您应该始终在功能完成之前删除创建的所有文件.

Secondly, you are not obliged to delete files out of tmpdir, but they occupy memory (it's a memory-based filesystem). Leaving those files around makes your future function invocations more expensive, since you are paying for memory over time. Also, you may risk running out of memory as you accumulate temp files. To be most efficient, you should always delete any files you create before the function finishes.

这篇关于删除Google Cloud Functions的tmp文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆