在Google Cloud Functions中写入文件后找不到文件 [英] Can't Find file after having written to it in Google Cloud Functions

查看:121
本文介绍了在Google Cloud Functions中写入文件后找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写os.tmpdir时,当我查看目录时可以看到该文件,但是在随后调用同一函数时却看不到任何东西.我认为这意味着该文件以某种方式不再存在(不太可能),或者我正在其他位置查找.我知道我应该在使用tmp目录后删除文件,但是在此之前,我希望能够再次找到这些文件,以确保所有删除方法都可以在以后使用.

When I write to os.tmpdir I can see the file when I look into the directory but on subsequent calls to same function I see nothing. I assume this means the file somehow no longer exists (unlikely) or I am looking in a different place. I know I should be deleting files in the tmp directory after I use them but before that I want to be able to find the files again to be assured that any deletion methods are working at a later stage.

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

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

推荐答案

Cloud Functions根据需要旋转环境以满足应用程序的负载,并在不再需要它们时将其旋转.本地存储特定于每个容器,并且不会在容器之间共享.

Cloud Functions spins up environments as needed to meet the load of your app, and spins them down when they are no longer needed. The local storage is specific to each container, and isn't shared between containers.

这意味着不能保证您写入本地存储的任何文件在以后调用Cloud Functions时都可用.因此,您应该只使用本地存储:

This means there is no guarantee that any file you write to local storage will be available on subsequent invocations of Cloud Functions. Therefor you should only use the local storage:

  • 对于一次调用中的临时文件,在这种情况下,应在函数退出时清理它们.
  • 用于缓存可以重新创建但价格昂贵的文件.例如.从数据库加载的内容.

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

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