32

I know Google App Engine offers free space, but I wonder if it's for storing data in its database only or does it also allow me to create files and directories on the server side to store my data ? For instance can I use the following method to save file ?

  public static void saveFile(String File_Path,StringBuffer Str_Buf,boolean Append)
  {
    FileOutputStream fos=null;
    BufferedOutputStream bos=null;

    try
    {
      fos=new FileOutputStream(File_Path,Append);
      bos=new BufferedOutputStream(fos);
      for (int j=0;j<Str_Buf.length();j++) bos.write(Str_Buf.charAt(j));
    }
    catch (Exception e) { e.printStackTrace(); }
    finally
    {
      try 
      {
        if (bos!=null)
        {
          bos.close();
          bos=null;
        }
        if (fos!=null)
        {
          fos.close();
          fos=null;
        }
      }
      catch (Exception ex) { ex.printStackTrace(); }
    }
  }
alex
  • 479,566
  • 201
  • 878
  • 984
Frank
  • 30,590
  • 58
  • 161
  • 244

6 Answers6

34

You can read files from your own project - You cannot write to the file system

from the FAQ ...

Why can't I write to this file?

Writing to local files is not supported in App Engine due to the distributed nature of your application. Instead, data which must be persisted should be stored in the distributed datastore. For more information see the documentation on the runtime sandbox

An App Engine application cannot:

  • write to the filesystem. Applications must use the App Engine datastore for storing persistent data. Reading from the filesystem is allowed, and all application files uploaded with the application are available.

  • open a socket or access another host directly. An application can use the App Engine URL fetch service to make HTTP and HTTPS requests to other hosts on ports 80 and 443, respectively.

  • spawn a sub-process or thread. A web request to an application must be handled in a single process within a few seconds. Processes that take a very long time to respond are terminated to avoid overloading the web server.

  • make other kinds of system calls.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • 1
    This is redundant now, you can now save to temp folders as far as I can tell. – Ari Jul 30 '19 at 06:41
6

New information. Answer is Yes, but you have to use their cloud storage for write access. You can not use regular files for your purpose.

https://developers.google.com/appengine/docs/java/googlecloudstorageclient/

It also has Python API as well as RESTful API.

Bin He
  • 136
  • 1
  • 2
5

No, the file i/o is not allowed. you may use blobs to store images or text.

Daniyar
  • 1,680
  • 2
  • 15
  • 23
3

There is a way if you use the /tmp folder. However, it will store files in the RAM of the instance, so note that it will take up memory and that it is temporary (as the folder's name suggest).

More details, see the documentation here or here.

In most situations, it is preferred to use Google Storage instead.

Note also that there is no issue to write to the file system if you choose the flexible environment instead of the standard one (however, be careful of the pricing difference, e.g. this).

hhh
  • 1,913
  • 3
  • 27
  • 35
2

I think it should be mentioned that writing to the blobstore using the files API is now deprecated, and that Google is moving to cloud storage.

Hari Ganesan
  • 532
  • 4
  • 18
2

Google App Engine is a scalable and stateless service.

  • Scalable means multiple parallel instances get started/shutdown/recycled (elastic).
  • If one instance writes data to a local SQLite DB, the other parallel instances can’t see or know about the data. This will lead to inconsistency.
  • You simply cannot have "local write" pattern in a stateless service.

Hence, you have to write your data (state) outside the app engine over the network to a durable storage (such as firebase,cloud sql,cloud storage,redis).

If you want the convenience of “all at one place” compute and local storage, you will have to spin a VM with durable block storage.

Thyag
  • 1,217
  • 13
  • 14