Blue
Blue

Reputation: 1448

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

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

Upvotes: 0

Views: 460

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

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.

Upvotes: 2

Related Questions