Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products. If you have feedback or questions as you navigate the site, click Send Feedback.

Reading and writing temporary files

Stay organized with collections Save and categorize content based on your preferences.

While Cloud Storage is the recommended solution for reading and writing files in App Engine, if your app only needs to write temporary files, you can use language-specific methods to write files to a directory named /tmp.

All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory. In addition, files in the /tmp directory are only available to the app instance that created the files. When the instance is deleted, the temporary files are deleted.

Go

Golang provides the TempDir and TempFile methods that read and write files to your app instance's /tmp directory by default.

Java

No additional information for this runtime.

Node.js

No additional information for this runtime.

PHP

If you want to write a temporary file to permanent storage, create the file with App Engine's version of tempnam(). This version of tempnam() returns an in-memory temporary file that can be written to a permanent storage solution such as Cloud Storage buckets.

Here is an example on how to create an in-memory file with tempnam(), and then write to the file using file_put_contents() and fwrite():

<?php
$dir = sys_get_temp_dir();
$tmp = tempnam($dir, "foo");
file_put_contents($tmp, "hello");
$f = fopen($tmp, "a");
fwrite($f, " world");
fclose($f);
echo file_get_contents($tmp);

This example will produce the following output:

hello world

Python

No additional information for this runtime.

Ruby

Ruby provides Dir and TempFile methods to read and write files to your app instance's /tmp directory.