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?
Asked
Active
Viewed 475 times
0
-
1Please share what you have tried so far. – new name Jan 23 '22 at 19:52
4 Answers
1
You have to get the body (the JSON) and save it in a cloud storage file. That should be enough

guillaume blaquiere
- 66,369
- 2
- 47
- 76
1
You need to write JSON file to /tmp
directory using fs.createWriteStream
and then write it to Storage using Storage API

xijdk
- 450
- 4
- 8
0
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);

Alex
- 778
- 1
- 15
0
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);
};

smoore4
- 4,520
- 3
- 36
- 55