LReddy
LReddy

Reputation: 137

How to update file when hosting in Google App Engine?

I have node js server service running on a Google Cloud App Engine. I have JSON file in the assets folder of the project that needs to update by the process. I was able to read the file and configs inside the file. But when adding the file getting Read-Only service error from the GAE.

Is there a way I could write the information to the file without using the cloud storage option ?

It a very small file and using the cloud storage thing would be using a very big drill machine for a Allen wrench screw

Thanks

Upvotes: 1

Views: 1083

Answers (2)

LReddy
LReddy

Reputation: 137

Once thanks for steering me not to waste time finding a hack solution for the problem.

Any way there was no clear code how to use the /tmp directory and download/upload the file using the app engine hosted node.js application. Here is the code if some one needs it

const {
    Storage
} = require('@google-cloud/storage');
const path = require('path');

class gStorage {
    constructor() {
        this.storage = new Storage({
            keyFile: 'Please add path to your key file'
        });
        this.bucket = this.storage.bucket(yourbucketname);
        this.filePath = path.join('..', '/tmp/YourFileDetails');
        // I am using the same file path and same file to download and upload
    }

    async uploadFile() {
        try {
            await this.bucket.upload(this.filePath, {
                contentType: "application/json"
            });
        } catch (error) {
            throw new Error(`Error when saving the config. Message :  ${error.message}`);
        }
    }

    async downloadFile() {
        try {
            await this.bucket.file(filename).download({
                destination: this.filePath
            });
        } catch (error) {
            throw new Error(`Error when saving the config. Message :  ${error.message}`);
        }
    }
}

Upvotes: 0

Puteri
Puteri

Reputation: 3789

Nope, in App Engine Standard there is no such a file system. In the docs, the following is mentioned:

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.

So having this consideration you can write in /tmp but I suggest to Cloud Storage because if the scaling shutdowns all the instances, the data will be lost.

Also you can think of App Engine Flex which offers to have a HDD (because its backend is a VM) but the minimum size is 10GB so it will be worst than using Storage.

Upvotes: 3

Related Questions