Can't Find file after having written to it in Google Cloud Functions

∥☆過路亽.° 提交于 2020-01-17 01:37:38

问题


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

回答1:


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.

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:

  • for temporary files within a single invocation, in which case you should clean them up when your function exits.
  • for caching files that can be recreated, but are expensive. E.g. things that you load from a database.


来源:https://stackoverflow.com/questions/51486490/cant-find-file-after-having-written-to-it-in-google-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!