简体   繁体   English

如何在 Google Cloud Functions 中获取当前的 projectId?

[英]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.我需要 projectId 才能建立连接 insde Google Cloud Function。我找到了文档,它说 projectId 是可选参数,将从 GCLOUD_PROJECT 检查,但在部署的 function 上它不起作用。

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?所以现在的问题是如何获取 projectId env 变量以便将其作为数据存储连接实例的参数传递,或者应该怎么做才能不传递此 ID 并与 Google Cloud Function 内的数据存储建立连接?

Update 1更新 1

I found that I actually can get variable from process.env.GCLOUD_PROJECT, just like any other env.variable.我发现我实际上可以从 process.env.GCLOUD_PROJECT 获取变量,就像任何其他 env.variable 一样。 But, know is the last question, is it actually possible to use @google-cloud/datastore without any configuration object?但是,知道是最后一个问题,是否真的可以在没有任何配置的情况下使用@google-cloud/datastore object?

You can get project ID as const projectId = process.env.GCP_PROJECT .您可以通过const projectId = process.env.GCP_PROJECT获取项目 ID。 But for some reason the environmet variable "GCP_PROJECT" is not set when running the emulator.但由于某种原因,运行模拟器时未设置环境变量“GCP_PROJECT”。 Instead the deprecated variable name "GCLOUD_PROJECT" is the one to select.相反,不推荐使用的变量名称“GCLOUD_PROJECT”是要选择的。 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.正如我在更新 1 中所写,我们可以从process.env变量中获取有关环境的所有信息。

And the second question about configuration object for @google-cloud/datastore , it actually can work without any options.关于@google-cloud/datastore 的配置对象的第二个问题,它实际上可以在没有任何选项的情况下工作。 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.在 Cloud Functions v2 公共预览版(2022 年 10 月)下,有一个名为内置参数的部分,允许require项目 ID、数据库 URL 和存储桶。

Presumably also Location ID ("Default GCP resource location") would be listed here, though that's not in the current documentation.大概也会在此处列出位置 ID(“默认 GCP 资源位置”),尽管当前文档中没有。

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.仅当存在活动的 Firebase 项目时才定义locationId This makes sense, since it needs access to the GCP project in the cloud.这是有道理的,因为它需要访问云中的 GCP 项目。

Firebase CLI 11.15.0 Firebase CLI 11.15.0

For any runtimes after node 8 you can use https://www.npmjs.com/package/gcp-metadata对于节点 8 之后的任何运行时,您可以使用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.如果您使用 Python 3.7 和 Go 1.11,那么您很幸运。 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')如果它是 HTTP function,我们可以使用 URL 通过req.header('host')找出当前项目和区域

When having deployed with firebase deploy there's:使用firebase deploy时:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在谷歌云中保存一些规则或配置,然后调用它来处理云函数中的发布订阅主题数据? - How can I save some rules or config in google cloud and then call it to process a pubsub topic data in cloud functions? 如何在Google Cloud Datastore .get上设置超时? - How can i set a timeout on google cloud datastore .get? Google Cloud Datastore GQL-获取当前日期时间 - Google cloud Datastore GQL - Get current datetime Google 云函数:如何在所需范围的云函数中获取访问令牌? - Google cloud functions: How to get access tokens in cloud function for the required scope? Google Cloud功能可以与Appengine共享数据存储吗? - Can Google Cloud functions share a datastore with Appengine? 如何使用graphql访问Google云数据存储? - How can I access a google cloud datastore using graphql? 谷歌云存储:我如何避免边缘缓存 - Google cloud storage : How can I Avoid edge cache 如何在Google Cloud / Python中按关键属性的属性排序? - How can I sort by an attribute of a key property in Google Cloud/Python? 如何将多个服务帐户凭据分配给 Google Cloud Functions? - How to assign multiple service account credentials to Google Cloud Functions? 如何从 Cloud Run 连接到 Google Cloud Datastore? - How can you connect to Google Cloud Datastore from Cloud Run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM