Search code examples
node.jsgoogle-app-enginegcloudgae-module

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


Solution

  • 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}`);
            }
        }
    }