英文:
How to set project wide variables
问题
有没有一种方法可以定义“项目范围”的环境变量?我知道你可以访问特定于函数的变量,例如
let url = process.env.DB_URL
let user = process.env.DB_USER
let password = process.env.DB_PASSWORD
但是我找不到任何关于项目范围变量的方法或文档。在上面的示例中,我有一些写入、读取...函数,它们都使用数据库凭据,我希望能够在项目级别指定这些凭据,而不是在每个函数中都指定一次。
英文:
Is there a way to define project wide
environment variables? I'm aware of the function specific variables that you can access like
let url = process.env.DB_URL
let user = process.env.DB_USER
let password = process.env.DB_PASSWORD
but I can't find any way or documentation for project wide variables. As I have write, read, ... functions that in the above example all use database credentials, I would like to specify those on project level instead for each function.
答案1
得分: 2
GCF本身不提供您所寻找的功能。您需要使用另一个GCP产品在项目中设置其他数据源,并从函数代码中查询它。
任何持久化数据并可查询的产品都可以,包括任何数据库。Firestore文档很容易设置和编辑。如果您希望您的代码能够快速响应数据源的更改,您将需要在每次函数调用时查询数据。
另一种选择是每次需要配置更改时部署每个函数,并编写一个脚本来设置每个函数部署的环境相同。这类似于Firebase默认使用环境变量部署函数的方式。
英文:
GCF on its own doesn't offer the thing you're looking for. You're going to have to set up some other source of data within your project using another GCP product, and query it from your function code.
Any product that persists data and is queryable will do, including any database. A Firestore document is easy enough to set up and edit. You will want to query the data on every function invocation if you want your code to respond to changes in the data source quickly.
Another alternative is to deploy every function every time you want a configuration change, and write a script that sets the environment the same for each function deployment. This is similar to the way Firebase deploys functions by default using environment variables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论