在Google Cloud Function中设置环境变量

时间:2021-08-10 00:11:58

I'm building a small node.js application that accepts POST requests and would upload the data to Google BigQuery.

我正在构建一个小的node.js应用程序,它接受POST请求并将数据上传到Google BigQuery。

BigQuery requires the GOOGLE_APPLICATION_CREDENTIALS to be set, else BigQuery will just timeout. As explained here: https://cloud.google.com/docs/authentication/production

BigQuery需要设置GOOGLE_APPLICATION_CREDENTIALS,否则BigQuery会超时。如下所述:https://cloud.google.com/docs/authentication/production

For example, making the query when the server was started as the following works as intended:

例如,在服务器启动时进行查询,如下所示:

GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json DEBUG=* bin/www

Making the query when the server was started as the following produces a timeout:

在服务器启动时进行查询,因为以下操作会产生超时:

DEBUG=* bin/www

I've tried the following npm packages to set environment variables within the script:

我尝试了以下npm包在脚本中设置环境变量:

"envs": "0.1.6",
"dotenv": "5.0.1"

And setting them as:

并将它们设置为:

var envs = require('envs');
app.set('environment', envs('GOOGLE_APPLICATION_CREDENTIALS', 'path/to/credentials.json')); 

and

require('dotenv').config()

but alas...

可惜...

Google Cloud function does not allow me to specify any environment variables, the script deployment logic is all behind the scenes. I've tried locating the config file in Google Cloud Console, and thought of setting it as a global environment variable, but I can't even locate it in that console.

Google Cloud功能不允许我指定任何环境变量,脚本部署逻辑都在幕后。我已经尝试在Google Cloud Console中找到配置文件,并考虑将其设置为全局环境变量,但我甚至无法在该控制台中找到它。

Any ideas?

有任何想法吗?

1 个解决方案

#1


2  

Unfortunately unlike AWS Lambda SCF doesn't support setting environment variables, at least in some obvious way.

不幸的是,不像AWS Lambda SCF不支持设置环境变量,至少以一些显而易见的方式。

But in your case you actually don't need that. You need GOOGLE_APPLICATION_CREDENTIALS on your local environment for testing, but on google cloud function it's enough to just set the project id in case you're using BigQuery npm

但在你的情况下,你实际上并不需要那样做。您需要在本地环境中进行GOOGLE_APPLICATION_CREDENTIALS进行测试,但在Google云端功能上,只需设置项目ID就可以了,以防您使用BigQuery npm

const BigQuery = require('@google-cloud/bigquery');

const projectId = 'project-id';

exports.testBigQuery = (req, res) => {

   const bigquery = new BigQuery({
       projectId: projectId,
   });


   bigquery
      .createDataset('test_set')
      .then(results => {
          const dataset = results[0];
          res.send(dataset.id);
      })
      .catch(err => {
          console.error('ERROR:', err);
      });
};

#1


2  

Unfortunately unlike AWS Lambda SCF doesn't support setting environment variables, at least in some obvious way.

不幸的是,不像AWS Lambda SCF不支持设置环境变量,至少以一些显而易见的方式。

But in your case you actually don't need that. You need GOOGLE_APPLICATION_CREDENTIALS on your local environment for testing, but on google cloud function it's enough to just set the project id in case you're using BigQuery npm

但在你的情况下,你实际上并不需要那样做。您需要在本地环境中进行GOOGLE_APPLICATION_CREDENTIALS进行测试,但在Google云端功能上,只需设置项目ID就可以了,以防您使用BigQuery npm

const BigQuery = require('@google-cloud/bigquery');

const projectId = 'project-id';

exports.testBigQuery = (req, res) => {

   const bigquery = new BigQuery({
       projectId: projectId,
   });


   bigquery
      .createDataset('test_set')
      .then(results => {
          const dataset = results[0];
          res.send(dataset.id);
      })
      .catch(err => {
          console.error('ERROR:', err);
      });
};