Write Variables to Google Cloud Storage
index.js
const functions = require('@google-cloud/functions-framework')
const { Storage } = require('@google-cloud/storage')
functions.http('endpoint', async (req, res) => {
const { authorization } = req.headers
// set a authorization header like this in the process module
if (authorization !== 'Bearer TTYz6n6q7tu8MhBV7TCp') {
res.sendStatus(403)
} else {
const vars = req?.body?.variables
const storage = new Storage()
await storage
.bucket('insert bucket name')
.file(`${Date.now()}.json`)
.save(JSON.stringify(vars))
res.setHeader('Content-type', 'application/json')
res.send({})
}
})
package.json
{
"name": "webservice_to_storage",
"version": "1.0.0",
"description": "WebService to Google Cloud Function to Google Storage Bucket",
"private": true,
"main": "index.js",
"scripts": {
"start": "npx functions-framework --target=endpoint --signature-type=http --port 8091"
},
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0",
"@google-cloud/storage": "^6.9.0"
}
}