【问题标题】:Authenticate google cloud run application in production在生产中验证谷歌云运行应用程序
【发布时间】:2020-07-09 07:15:08
【问题描述】:

根据https://cloud.google.com/docs/authentication/production,在Cloud Run环境中,默认情况下,如果没有设置GOOGLE_APPLICATION_CREDENTIALS环境变量,则使用默认的Compute Service账号。

我刚刚部署了一个基本的节点容器镜像,它使用googleapis 库,如下所示:

const { google } = require('googleapis')
const auth = new google.auth.GoogleAuth({
  scopes: [],
});
const client = await auth.getClient();

但是,在 Cloud Run 日志中,我可以看到容器无法启动并出现以下错误:

Error: The file at credentials/service_account.json does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/app/credentials' at Object.realpathSync (fs.js:1546:7) at GoogleAuth._getApplicationCredentialsFromFilePath (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:250:27) at GoogleAuth._tryGetApplicationCredentialsFromEnvironmentVariable (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:192:25) at GoogleAuth.getApplicationDefaultAsync (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:130:33) at GoogleAuth.getClient (/app/node_modules/google-auth-library/build/src/auth/googleauth.js:502:28) at Object.authorize (/app/api.js:18:29) at Object.<anonymous> (/app/index.js:547:4) at Module._compile (internal/modules/cjs/loader.js:1158:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) {

因此,出于某种原因,似乎没有使用默认服务帐户。有谁知道我在这里可能缺少什么?

【问题讨论】:

  • 您的代码中的某些内容正在设置credentials/service_account.json。您只发布部分代码,没有部署文件。
  • @JohnHanley 你是对的。我搜索了源代码,并没有找到它。原来我将它设置在.env 文件中。感谢您的建议。

标签: node.js google-cloud-platform google-api-nodejs-client google-cloud-run


【解决方案1】:

原来,我在构建中包含了.env 文件。该文件包含环境变量GOOGLE_APPLICATION_CREDENTIALS。从 docker build 中排除该文件修复了该问题。

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 2020-10-03
    • 2020-10-28
    • 2020-08-25
    • 2021-10-02
    • 2020-09-03
    • 1970-01-01
    • 2017-05-24
    • 2019-08-07
    相关资源
    最近更新 更多