简体   繁体   中英

How to save data from app engine to datastore google cloud javascript

I'm new to GCP and it's so difficult for me to understand it's documents. I deployed my web app on App Engine. When I run my app locally, I save some data on a JSON file and it's perfect. Now I need to save that JSON which is coming from client side to somewhere on Google cloud.

Based on my research I need to store my data on datastore. I need some clear example and explanation to learn how to store data from App Engine to data store in GCP. Basically I'm looking for a way to store my JSON to pass it to another app later. I appreciate any help or suggestion.

 const port = process.env.PORT || 8000;
 app.use(express.static(__dirname + '/www'));

 app.listen(port);
 console.log('working on port '+ port);

 app.use(express.json({limit:'1mb'}));
 app.post('/api', (request, response) => {

     var ressult = JSON.stringify(request.body);

     //creating my JSON file
     fs.appendFile('Result.json', ressult +  "\n", (err) => { 

     if (err) throw err; 
 })     

});

So first of all the basics:

You'll need something like the following to initialize the client:

// Imports the Google Cloud client library
const {Datastore} = require('@google-cloud/datastore');

// Creates a client
const datastore = new Datastore();

Then to create a basic entity:

async function quickstart() {
  // The kind for the new entity
  const kind = 'Task';

  // The name/ID for the new entity
  const name = 'sampletask1';

  // The Cloud Datastore key for the new entity
  const taskKey = datastore.key([kind, name]);

  // Prepares the new entity
  const task = {
    key: taskKey,
    data: {
      description: 'Buy milk',
    },
  };

  // Saves the entity
  await datastore.save(task);
  console.log(`Saved ${task.key.name}: ${task.data.description}`);
}
quickstart();

So now that you can create the basic entity you have different options. If the JSON object is not too big you can put it as a value in the entity (store it as text)

Or the better approach is to store it as an array using something like this:

  testArrayValue() {
    // [START datastore_array_value]
    const task = {
      tags: ['fun', 'programming'],
      collaborators: ['alice', 'bob'],
    };
    // [END datastore_array_value]

    return this.datastore.save({
      key: this.incompleteKey,
      data: task,
    });
  }

Depending on your JSON you might even want to create nested arrays but the logic is the same.

You can also use Cloud Storage instead and simply treat the JSON file as an object. So you'll need to store it in the /tmp directory of GAE, upload it to the bucket. Then on the other side, download it to the /tmp dir of that app, and process it as a JSON file. Here are the basics on how to get started with Cloud Storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM