简体   繁体   English

如何读取 app.yaml 中的环境变量?

[英]How to read environmental variables in app.yaml?

I am using Google App Engine and have an app.yaml file that looks like this:我正在使用 Google App Engine 并有一个 app.yaml 文件,如下所示:

runtime: go115

env_variables:
  INSTANCE_CONNECTION_NAME: secret:northamerica-northeast1:special
  DB_USER: db_username
  DB_PASS: p@ssword
  DB_NAME: superduper

Using this Github as a refernce, I am using this to read those values:使用这个Github作为参考,我用它来读取这些值:

dbUser = mustGetenv("DB_USER") dbUser = mustGetenv("DB_USER")

I do a gcloud app deploy and then check for the variable with the set command.我执行 gcloud 应用程序部署,然后使用 set 命令检查变量。 I do not see the variable there, and likewise, when my program runs it can not find the variable.我在那里看不到变量,同样,当我的程序运行时,它找不到变量。

I seem to be doing everything the example is doing, but my variable is not being found by my program.我似乎正在做示例所做的一切,但我的程序没有找到我的变量。 Is there any specific formating of the yaml file? yaml 文件有什么特定的格式吗? Is there some other step I need to be doing before trying to read the variable?在尝试读取变量之前,我还需要做什么其他步骤吗?

您可以使用os.Getenv读取变量。

dbUser = os.Getenv("DB_USER")

You can try using viper https://github.com/spf13/viper您可以尝试使用毒蛇https://github.com/spf13/viper

func Init() {
  // Set the file name of the configurations file
  viper.SetConfigName("app")

  // Set the path to look for the configurations file
  viper.AddConfigPath("./config")

  // Enable VIPER to read Environment Variables
  viper.AutomaticEnv()

  viper.SetConfigType("yaml")

  if err := viper.ReadInConfig(); err != nil {
      fmt.Printf("Error reading config file, %s", err)
  }
}

Add this initialisation in you main then you can access using following anywhere in code在您的 main 中添加此初始化,然后您可以在代码中的任何位置使用以下内容进行访问

viper.GetString("env_variables.INSTANCE_CONNECTION_NAME")

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

相关问题 如何在 app.yaml 中为 Go 传递运行 arguments? - How to pass run arguments in app.yaml for Go? 在app.yaml上指定的环境变量,但它不在main.go上获取 - Environment variables specified on app.yaml but it's not fetching on main.go 在app.yaml env_variables中,无法处理换行代码 - In app.yaml env_variables, the line feed code can not be handled 如何在不同于app.yaml的文件夹中上载Google App Engine(Go)项目 - How to upload a Google App Engine (Go) project in a different folder than the app.yaml 如何在 Win 10 上本地测试 Gol App Engine 应用并使用 app.yaml - How to Test Gol App Engine apps locally on Win 10 and use app.yaml 如何在appengine Go中使用与app.yaml不同的文件夹中的主包? - How to use main package in different folder than app.yaml for appengine Go? 为什么应用引擎会忽略app.yaml中的PORT env变量 - Why app engine ignore PORT env variable in app.yaml 将app.yaml从go114更新到go115时如何处理`app_engine_apis`警告 - How to deal with `app_engine_apis` warning when updating app.yaml from go114 to go115 app.yaml:带有static_dir的URL中的通配符? - app.yaml : wildcard in URL with static_dir? gcloud app deploy 找不到 app.yaml 如果 go.mod 在不同的目录中 - gcloud app deploy cannot find app.yaml if go.mod is in a different directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM