how to set environment variables on google appengi

2019-06-26 02:33发布

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'

3条回答
不美不萌又怎样
2楼-- · 2019-06-26 03:15

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

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

import "os"

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

It does not work if you declare as global variable.

查看更多
我命由我不由天
3楼-- · 2019-06-26 03:24

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

查看更多
SAY GOODBYE
4楼-- · 2019-06-26 03:28

Unfortunately, the GAE Go runtime does not support environment variable setting in app.yaml -- see for example 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&q=language=Go&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).

查看更多
登录 后发表回答