Created
March 20, 2019 21:59
-
-
Save devilelephant/0925d390593b0f25f0015935a26d93f4 to your computer and use it in GitHub Desktop.
Google Cloud Environment Credentials
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
// 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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.