quicklikerabbit
quicklikerabbit

Reputation: 3556

Setting Heroku Config Var with contents from Google Cloud Service Account Keyfile

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: enter image description here

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?

Upvotes: 6

Views: 2149

Answers (2)

Sylvia Pap
Sylvia Pap

Reputation: 336

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.

Upvotes: 1

Robert Moskal
Robert Moskal

Reputation: 22553

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)
});

Upvotes: 7

Related Questions