3

I managed to get the "Quickstart for Python 3 in the App Engine Standard Environment" example up and running, and I thought I'd try and further my knowledge a little, perhaps by attempting to get a cron job running.

So I updated the python code, adding another endpoint, counter, like this:

@app.route('/counter')
def counter():
    with open('counter.txt', 'a') as the_file:
        the_file.write('Hello\n')

    return 'Counter incremented'

I intend to have the cron job periodically hit /counter. When this endpoint is hit it will open the file and add a line to it. The /counter endpoint works on my local machine. After I deploy this updated code to the Google cloud, if I go to my blahblah.appspot.com/counter url it should update this 'counter.txt' file.

My question is: How do I see that file to know if it is being updated or not? How do I view that file in the cloud? Thanks.

Lord Null
  • 856
  • 1
  • 9
  • 20

3 Answers3

4

It not possible to write files in the Google App Engine Standard Python3 environment except in the /tmp directory. As stated in the Python3 GAE official documentation:

The runtime includes a full filesystem. The filesystem is read-only except for the location /tmp, which is a virtual disk storing data in your App Engine instance's RAM.

I agree with @Josh J answer, you should use Google Cloud Storage instead.

llompalles
  • 3,072
  • 11
  • 20
1

You shouldn't write to the local file system in Google App Engine. It may or may not be visible to your app when it scales.

Google Cloud Storage is the preferred method of file storage.

https://cloud.google.com/appengine/docs/standard/python3/using-cloud-storage

Josh J
  • 6,813
  • 3
  • 25
  • 47
0

For python 2.7 you can import a module from string instead of file, you can see in this answer: https://stackoverflow.com/a/7548190/8244338

Israel
  • 1,165
  • 11
  • 11