Last active
June 26, 2017 17:48
-
-
Save JustinBeckwith/6b8d999b09188508f3d04ba27cf46d44 to your computer and use it in GitHub Desktop.
Bounce a file with GCF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const gcs = require('@google-cloud/storage')(); | |
const uuid = require('uuid/v4'); | |
const bucket = gcs.bucket('el-gato-functions'); | |
exports.bounce = (req, res) => { | |
const bounceUrl = req.get('bounceUrl') || 'https://www.googleapis.com/discovery/v1/apis/'; | |
console.log(`requesting ${bounceUrl}`); | |
request(bounceUrl, (err, response, body) => { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
const data = JSON.parse(body); | |
// this is where you can modify `data` if you need to | |
const tempFileName = uuid(); | |
const file = bucket.file(tempFileName); | |
let stream = file.createWriteStream() | |
.on('error', (err) => { | |
console.error("There was a problem uploading the file to GCS :("); | |
console.error(err); | |
}) | |
.on('finish', () => { | |
console.log("File upload complete!"); | |
res.send(`Written to ${tempFileName}`); | |
}); | |
stream.end(JSON.stringify(data)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment