I have a web app that I'm trying to deploy with Heroku. I'm using the Google Cloud Node.js library for accessing google cloud storage. I have the following at the top of my server file:
const gcs = require('@google-cloud/storage')({
projectId: 'my-project-ID',
credentials: process.env.GCS_KEYFILE
});
GCS_KEYFILE is an configuration variable that I've set using the Heroku command line tools using heroku config:set GCS_KEYFILE="$(< /my/file.json)"
Checking the dashboard to make sure that worked confirms the contents of the JSON file have been set to a config var. Screenshot looks like this:
The error I get when I try to do anything with gcs
is:
Error: Could not authenticate request
The incoming JSON object does not contain a client_email field
Which makes no sense because it clearly does. The json itself is fine; I took the download directly from Google Cloud without modifying it. I've tried using keyFileName
in my const gcs
declaration but I get a ENAMETOOLONG
error (plus the docs say thats for specifying the path to the JSON file anyway). It works locally when I use keyFileName
and specify the path to the JSON file so I'm pretty sure the JSON itself not the issue.
Any ideas as to why I'm getting this error? Or is there a better way to handle JSON Keyfiles from Google on Heroku?
One way of setting the environment variables on Heroku is to use the web interface. The first step is to log into your account and go to the Heroku dashboard. Figure 1 illustrates my dashboard. Choose the application for which you want to set the environment variables.
Heroku's environment variables are called config vars. To set a config var, navigate to the Settings tab of your app's dashboard, and click Reveal Config Vars.
Environment variables on heroku, or anywhere, are strings.
If that google api constructor takes a JSON object you may need to do something like this to convert it back to JSON:
const gcs = require('@google-cloud/storage')({
projectId: 'my-project-ID',
credentials: JSON.parse(process.env.GCS_KEYFILE)
});
There is a helpful heroku buildpack for this
heroku buildpacks:add --index 1 https://github.com/buyersight/heroku-google-application-credentials-buildpack.git
--index 1
is because "The buildpack for the primary language of your app should be the last buildpack added," so if you have other buildpacks be careful of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With