firebase - Two identical GCP Projects but in one the Cloud Functions fails to load the default credentials
问题描述
I'm running the same Cloud Function in 2 different GCP Projects (staging and production).
In the staging project, the Cloud Functions runs well but in the production it throws the following exception:
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)"
The part that is causing the problem in production, is this:
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)
});
}
Here is how I initiate the project and storage.
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
// Firebase related stuff
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
Since I deploy the same function to both projects I don't understand why it is having issues in the production, and not staging.
What is usually causing the issue with default credentials? I followed the link in the trace but its not relevant to the actual problem.
Thanks.
解决方案
如果你想使用默认凭据,你应该像这样初始化 Admin SDK,不带参数:
const admin = require('firebase-admin');
admin.initializeApp();
推荐阅读
- javascript - 如何从二维数组中提取多列?
- excel - 从用户窗体 VBA 编辑时维护编辑历史记录
- linux - Bash:使用 systemd 的日期时代
- spring-boot - Angular 9 - 如何正确编码+登录URL参数
- excel - If And - and 语句
- entity-framework - POST 导致 HTTP 415 Unsupported Media Type 响应
- visual-studio-code - 在 Visual Studio Code 中配置 nuget 包管理器的包源?
- postgresql - Grafana 将时区差异添加到日期
- python - 为什么 PyGame 没有生成新字母?
- json - Azure 数据资源管理器/Kusto JSON 摄取转换 (GetPathElement)