【问题标题】:Two identical GCP Projects but in one the Cloud Functions fails to load the default credentials两个相同的 GCP 项目,但其中一个 Cloud Functions 无法加载默认凭据
【发布时间】:2020-03-01 15:26:31
【问题描述】:

我在 2 个不同的 GCP 项目(暂存和生产)中运行相同的云函数。

在暂存项目中,Cloud Functions 运行良好,但在生产中它会引发以下异常:

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/srv/node_modules/@google-cloud/common/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)" 

在生产中导致问题的部分是:

function saveContentToBucket(contents, destination, bucket, gzip){
  const bucket = storage.bucket(FIRESTORE_BUCKET_NAME);
  const file = bucket.file(destination);
  file.save(JSON.stringify(contents), function(err) {
    if(err) console.log("saveContentToBucket | err:", err)
  });
}

这是我启动项目和存储的方式。

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

// Firebase related stuff
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);

由于我将相同的功能部署到两个项目,我不明白为什么它在生产中出现问题,而不是暂存。

通常是什么导致默认凭据出现问题? 我跟踪了跟踪中的链接,但它与实际问题无关。

谢谢。

【问题讨论】:

  • 几乎肯定不相关,但FIRESTORE_BUCKET_NAME是如何定义的?
  • 它是一个变量,在 staging env 中工作。我相信我使用@Doug 回答解决了它。

标签: firebase google-cloud-platform google-cloud-storage firebase-storage


【解决方案1】:

如果您想使用默认凭据,您应该像这样初始化 Admin SDK,不带任何参数:

const admin = require('firebase-admin');
admin.initializeApp();

【讨论】:

  • 我明白,但它是如何在 Staging 项目中工作的,在我的情况下,然后使用 firebase 凭据?
  • 我不知道 - 我看不到您项目的配置。
猜你喜欢
  • 2021-01-06
  • 2018-02-20
  • 2017-07-13
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 2022-11-18
相关资源
最近更新 更多