Skip to content

Instantly share code, notes, and snippets.

@devilelephant
Created March 20, 2019 21:59
Show Gist options
  • Save devilelephant/0925d390593b0f25f0015935a26d93f4 to your computer and use it in GitHub Desktop.
Save devilelephant/0925d390593b0f25f0015935a26d93f4 to your computer and use it in GitHub Desktop.
Google Cloud Environment Credentials
// Configure Google Cloud library with JSON credentials in an environment variable
const
os = require('os'),
fs = require('fs')
;
exports.configureGoogleCloud = function (isWarnOnly) {
// if standard GOOGLE_APPLICATION_CREDENTIALS is set then use that
if (!process.env['GOOGLE_APPLICATION_CREDENTIALS']) {
// Grab the JSON from the environment variable and save it to disk so Google library can find it.
const env_creds = process.env['GOOGLE_APPLICATION_CREDENTIALS_JSON']
if (env_creds) {
const creds_file = os.tmpdir() + '/credentials.json'
fs.writeFileSync(creds_file, env_creds)
// Google Cloud's magic environment variable
process.env['GOOGLE_APPLICATION_CREDENTIALS'] = creds_file
} else {
const message = 'WARN: Google application credentials not found'
if (isWarnOnly) {
console.log(message)
} else {
throw new Error(message)
}
}
}
}
@devilelephant
Copy link
Author

Configuring the Google Cloud library is annoying because it requires us include a credentials.json file with every project vs just setting environment variables like Amazon AWS. This script fixes that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment