shanec
shanec

Reputation: 15

How to set file permissions in google cloud app engine

How can I change the file permissions of a .json to allow all website visitors to write to it?

I have a PHP based website in google cloud app engine that makes a basic phone book that is editable using a .json to store the data. Once you login, You can add, delete, and change entries to the phonebook. I've done the following:

Here is the important php code

$dbFile = 'phoneBook.json';
$json = file_get_contents($dbFile);
$depth = 4;
$phBook = json_decode($json, true, $depth);

// ...manipulation...

$fp = fopen($dbFile, 'w');
fwrite($fp, json_encode($phBook));
fclose($fp);

And i use that to manipulate the phonebook.json

Here is my app.yaml

runtime: php55
api_version: 1
threadsafe: true

runtime_config:
  document_root: web

handlers:

- url: /(.+\.php)$
  script: \1 

- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)

- url: /www/checklogin.php
  script: checklogin.php

The phonebook.json is in same directory as the other php files. In linux all i have to do is chmod and change the permissions to allow writes, is there anyway i can do that with what i have here? I read somewhere that you can do it if you use the compute engine VM instance instead of the app engine. is that my only choice?

Upvotes: 0

Views: 1014

Answers (1)

mensi
mensi

Reputation: 9836

You should not attempt to store data in local files. App Engine is a "serverless" type of service - depending on your scaling settings, it will spin up instances as required to serve the traffic. It will also replace instances for upgrades or if they become unhealthy.

As such, you will have quite a chaos: Some requests might go to different instances, therefore showing a different view on your phonebook.json as they will locally load the file. Even if you restrict scaling to only one instance, you will lose all your data periodically when upgrades are performed.

Instead, use something like CloudSQL to store data. There, all serving instances will be able to access the same data and you won't lose anything during upgrades.

Upvotes: 2

Related Questions