首页 > 解决方案 > 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.

标签: firebasegoogle-cloud-platformgoogle-cloud-storagefirebase-storage

解决方案


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

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

推荐阅读