简体   繁体   中英

How can I get current projectId inside Google Cloud Functions?

The question is simple. I need projectId in order to establish connection insde Google Cloud Function. I found the documentation where it said, that projectId is optional parameter and will be checked from GCLOUD_PROJECT, but on deployed function it didn't work.

So it is the question now how can I get the projectId env variable in order to pass it as argument for Datastore connection instance, or what should be done to not pass this id and establish connection with datastore inside Google Cloud Function?

Update 1

I found that I actually can get variable from process.env.GCLOUD_PROJECT, just like any other env.variable. But, know is the last question, is it actually possible to use @google-cloud/datastore without any configuration object?

You can get project ID as const projectId = process.env.GCP_PROJECT . But for some reason the environmet variable "GCP_PROJECT" is not set when running the emulator. Instead the deprecated variable name "GCLOUD_PROJECT" is the one to select. So you might try them both.

const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT

As I wrote in update 1 we can get all the information about environment from the process.env variable.

And the second question about configuration object for @google-cloud/datastore , it actually can work without any options. It will trying to fetch all required parameters from environment variables. So, it didn't work beacuase of error in my code.

Under Cloud Functions v2 public preview (Oct 2022), there's a section called built-in parameters that allows require of Project ID, Database URL and Storage bucket.

Presumably also Location ID ("Default GCP resource location") would be listed here, though that's not in the current documentation.

I did not get that to work.

However, this does:

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

const locationId_maybe = firebaseConfig.locationId || null;
const projectId = firebaseConfig.projectId;
const databaseURL = firebaseConfig.databaseURL;

The env.var. is always defined. locationId is defined only if there's an active Firebase project. This makes sense, since it needs access to the GCP project in the cloud.

Firebase CLI 11.15.0

For any runtimes after node 8 you can use https://www.npmjs.com/package/gcp-metadata

const gcpMetadata = require('gcp-metadata');

export const getGCPProjectIdFromMetadata = async () => {
    const isAvailable = await gcpMetadata.isAvailable();

    if (!isAvailable) {
        return undefined;
    }

    return gcpMetadata.project('project-id');
};

This will also work across any other instances you wish to launch (such as app engine).

The answer depends on which runtime you are using. If your using Python 3.7 and Go 1.11 you are in luck. Use

process.env.GCP_PROJECT

if it's an HTTP function, we can use the URL to figure out the current project and region with req.header('host')

When having deployed with firebase deploy there's:

functions.config().firebase.projectId
functions.config().firebase.databaseURL
functions.config().firebase.storageBucket
functions.config().firebase.locationId

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