简体   繁体   中英

How to get Firebase Project Name or ID from Cloud Function

I am using Cloud Functions and want to get the project name from within one of my Javascript server files. I know that value is stored in the .firebaserc, but I don't think that file is available on the server, right? I want to do something like this:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.getProjectName();  // or getProjectID()

or

functions.getProjectName(); 

Thank you @Frank. The answer is: process.env.GCLOUD_PROJECT .
I'm not sure where the variable process comes from, but this does work to get the project name.

Firebase admin SDK has access to that information for you.

const admin = require('firebase-admin');
const projectId = admin.instanceId().app.options.projectId

This worked for me. (node 10)

const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG && JSON.parse(process.env.FIREBASE_CONFIG);
const projectId = FIREBASE_CONFIG.projectId;

试试这个 :

const projectId = admin.instanceId().app['options_'].credential.projectId

Currently I'm using app.INTERNAL.credential_.projectId . This is clearly not save, but so far there is no cohesive way to get it (AFAIK)

This is a combination of the answers given above:

function getProjectID() {
  return firebaseInstance.options.projectId ||
    (firebaseInstance.options.credential && firebaseInstance.options.credential.projectId) || '';
}

Or in TypeScript:

function getProjectID(): string {
  return firebaseInstance.options.projectId ||
    (firebaseInstance.options.credential && (firebaseInstance.options.credential as unknown as { projectId: string }).projectId) || '';
}

For cloud functions running on newer versions of Node:

import firestore from "@google-cloud/firestore";

const client = new firestore.v1.FirestoreAdminClient();
const projectId = await client.getProjectId();

From the Firebase docs :

process.env.FIREBASE_CONFIG : Provides the following Firebase project config info:

 { databaseURL: 'https://databaseName.firebaseio.com', storageBucket: 'projectId.appspot.com', projectId: 'projectId' }

Note that since it's a JSON string you need to parse it like this:

const projectId = JSON.parse(process.env.FIREBASE_CONFIG).projectId;

Alternatively you could use the undocumented firebaseConfig() method from the firebase-functions package in your Cloud Functions, like so:

import { firebaseConfig } from "firebase-functions";
const projectId = firebaseConfig().projectId;

Additionally, Google Cloud Platform (which Firebase runs on top of) will automatically populate the environment variables documented here , depending on which runtime you're using.

The way to retrieve the project ID using admin SDK is

admin.appCheck().app.options.credential.projectId

This also works in headless setups like Docker where the environment variable GOOGLE_APPLICATION_CREDENTIALS is set.

You can use functions.config().firebase.projectId

PS the easiest way to initialize app is admin.initializeApp(functions.config().firebase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM