将Google API密钥文件添加到Heroku [英] Add Google API keyfile to Heroku

查看:66
本文介绍了将Google API密钥文件添加到Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Heroku上托管API,一切都进行得很顺利,但是我无法完成设置,因为Google Cloud Language API SDK需要文件的路径,而不仅仅是文件中的值.

I'm trying to host an API on Heroku, everything is going very smooth, but I cannot complete the setup because the Google Cloud Language API SDK requires the path to a file, rather than just the values within the file.

我已经为我正在使用的所有服务成功设置了环境变量.我试图将文件的内容添加到环境变量中:

I have successfully set environment variables for all of the services I am using. I have tried to add the contents of the file to an environment variable:

heroku config:set GOOGLE_KEY_FILE={....}

这不起作用,因为它需要一个文件路径.

This does not work, as it expects a filepath.

提交文件的快捷方式(我显然做不到),如何将文件放入Heroku的文件系统中,以便我的API可以读取它?

Short of committing the file, which I obviously can't do, how can I get the file into Heroku's file system so that my API can read it?

或者还有另一种方法可以完成,默认情况下,以下命令在本地运行良好:

Or is there another way this can be done, the following runs fine locally when it defaults to a filepath:

const initialiseLanguage = require('@google-cloud/language')
const {
  GOOGLE_PROJECT_ID: projectId,
  GOOGLE_KEY_FILE: keyFilename = './side-project-xxxxxxxxx.json'
} = process.env

const language = initialiseLanguage({
  projectId,
  keyFilename
})

谢谢.

推荐答案

在我的应用程序中也使用GCS密钥文件,并且我还将密钥文件JSON设置为环境变量,所以我要这样做:

In my application uses GCS key file too, and I also set the key file JSON into a environment variable, so that's what I do:

const fs = require('fs');
const path = require('path');
const gTokenPath = path.join(`${__dirname}/gToken.json`);
fs.writeFileSync(gTokenPath , process.env.GCS_JSON_TOKEN);

const gcsKeyFile = JSON.parse(process.env.GCS_JSON_TOKEN);

const language = initialiseLanguage({
  projectId: gcsKeyFile.project_id,
  keyFilename: gTokenPath,
})

这篇关于将Google API密钥文件添加到Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆