简体   繁体   中英

How to download json file to cloud storage on app engine node.js standard environment

I have a web application on GAE node.js standard environment. The server receives a POST request containing json in its body (not ready json file). I want to write this json file to Cloud Storage. How to do this?

You have to get the body (the JSON) and save it in a cloud storage file . That should be enough

To consume the body of your post request, you can check a previous discussion here . On the other hand, if you specifically want to convert it to a JSON file, check this other post . Continuing with the upload, you can consult the documentation example where this is the suggested procedure (please visit the page for the full script for the recommended variable paths and the links to the Cloud Storage Node.js API reference):

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

// Creates a client
const storage = new Storage();

async function uploadFile() {
  await storage.bucket(bucketName).upload(filePath, {
    destination: destFileName,
  });

  console.log(`${filePath} uploaded to ${bucketName}`);
}

uploadFile().catch(console.error);

You need to write JSON file to /tmp directory using fs.createWriteStream and then write it to Storage using Storage API

The key is to use req.rawBody not req.body . Here is a full working example:

exports.getData = (req, res) => {

// The ID of your GCS bucket
const bucketName = 'yourBucket';
// The new ID for your GCS file
const destFileName = 'yourFileName';
// The content to be uploaded in the GCS file
const contents = req.rawBody;
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Import Node.js stream
const stream = require('stream');
// Creates a client
const storage = new Storage();
// Get a reference to the bucket
const myBucket = storage.bucket(bucketName);
// Create a reference to a file object
const file = myBucket.file(destFileName);

// Create a pass through stream from a string
const passthroughStream = new stream.PassThrough();
passthroughStream.write(contents);
passthroughStream.end();

async function streamFileUpload() {
  passthroughStream.pipe(file.createWriteStream()).on('finish', () => {
    res.status(200).send('OK');
  });

  console.log(`${destFileName} uploaded to ${bucketName}`);
}

streamFileUpload().catch(console.error);

};

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