how to set environment variables on google appengine?

huangapple go评论72阅读模式
英文:

how to set environment variables on google appengine?

问题

我正在尝试在Google App Engine上设置和使用环境变量。我的app.yaml文件如下所示。然而,当我使用os.Getenv("mytoken")时,我得到的是一个空字符串,而不是我设置的实际值。这是一个GAE的bug吗?

api_version: go1
handlers:

  • url: /.*
    script: _go_app
    env_variables:
    mytoken: '88786d9b9a0359824'
英文:

I'm trying to set and use an environment variable on google app engine. My app.yaml file looks as below. However when I use os.Getenv("mytoken") I get an empty string instead the actual value I set. Is it a GAE bug?

api_version: go1
handlers:
- url: /.*
  script: _go_app
env_variables:
  mytoken: '88786d9b9a0359824'

答案1

得分: 8

这个功能现在已经在这里有文档记录。然而,正如我刚刚测试的,它在AppEngine上不起作用。它在本地服务器上是有效的,所以不要被欺骗。

编辑:它在Google AppEngine上是有效的。我之前的失败是由于一个错误。考虑以下代码:

import "os"

var consumer = OAuth1Consumer{
    secret: os.Getenv("secret")
}

如果你将其声明为全局变量,它是不起作用的。

英文:

The feature is now documented here. <strike>However, as I have tested just now, it doesn't work on AppEngine. It does work on local server, so don't be fooled.</strike>.

Edit: It works on Google AppEngine. My previous failure is due to a mistake. Consider this:

import &quot;os&quot;

var consumer = OAuth1Consumer{
    secret: os.Getenv(&quot;secret&quot;)
}

It does not work if you declare as global variable.

答案2

得分: 2

很抱歉,GAE Go运行时不支持在app.yaml中设置环境变量。你可以参考以下链接了解更多信息:https://stackoverflow.com/questions/24585406/how-to-set-gae-environment-specific-environment-variables 和 https://groups.google.com/forum/#!msg/google-appengine-go/qzMbZapLyAU/eKOZzZO14qQJ

然而,PHP(https://cloud.google.com/appengine/docs/php/config/appconfig#PHP_app_yaml_Defining_environment_variables)、Java(https://cloud.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_System_properties_and_environment_variables)和Python(https://cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables)运行时都支持这个功能。

在App Engine的Go运行时中(https://cloud.google.com/appengine/docs/go/config/appconfig),很遗憾,并没有类似的功能。

我建议你在https://code.google.com/p/googleappengine/issues/list?can=2&q=language=Go&colspec=ID%20Type%20Component%20Status%20Stars%20Summary%20Language%20Priority%20Owner%20Log 提出一个功能请求(我没有在27个未解决的功能请求中找到类似的请求,当然,如果有的话,我会建议你给现有的功能请求点赞以表明你对它的兴趣)。

英文:

Unfortunately, the GAE Go runtime does not support environment variable setting in app.yaml -- see for example https://stackoverflow.com/questions/24585406/how-to-set-gae-environment-specific-environment-variables and https://groups.google.com/forum/#!msg/google-appengine-go/qzMbZapLyAU/eKOZzZO14qQJ .

The functionality is supported in PHP, per https://cloud.google.com/appengine/docs/php/config/appconfig#PHP_app_yaml_Defining_environment_variables ; Java, per https://cloud.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_System_properties_and_environment_variables ; and Python, per https://cloud.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Defining_environment_variables .

In the Go runtime for App Engine, however -- see https://cloud.google.com/appengine/docs/go/config/appconfig -- there is simply no equivalent functionality.

I would recommend opening a feature request at https://code.google.com/p/googleappengine/issues/list?can=2&amp;q=language=Go&amp;colspec=ID%20Type%20Component%20Status%20Stars%20Summary%20Language%20Priority%20Owner%20Log (I don't see any equivalent feature request already in the list of 27 open FRs, or else, of course, I would recommend just "starring" the existing FR to register your interest in it).

答案3

得分: 0

我无法在任何地方找到该字段的文档。我只在Python配置中看到它,而不是Go配置。如果你是根据Python文档这里来进行操作的话,很可能在Go中不可用。

英文:

I can't find the docs for that field anywhere. I only see it in the python config, not the Go config. It's likely unavailable for go if you're basing this off of the python docs here

huangapple
  • 本文由 发表于 2015年3月22日 01:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/29186031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定