确定我的App Engine代码正在运行的项目ID

时间:2014-08-28 15:33:33

标签: python google-app-engine google-bigquery google-cloud-platform

在App Engine应用程序中,有没有办法确定运行GAE(App Engine)实例的项目ID?

我想在运行App Engine实例的同一项目中访问一个大的查询表。如果可能的话,我不是硬编码或者将其包含在另一个配置文件中。

编辑:忘了提到这是来自Python

4 个答案:

答案 0 :(得分:11)

这是“官方”方式:

from google.appengine.api import app_identity

GAE_APP_ID = app_identity.get_application_id()

在此处查看更多内容:https://developers.google.com/appengine/docs/python/appidentity/

答案 1 :(得分:8)

您可以从环境变量中获取大量信息:

import os
print os.getenv('APPLICATION_ID')
print os.getenv('CURRENT_VERSION_ID')
print os.environ

答案 2 :(得分:4)

我还添加了一个应用版本,以防您需要它。

import com.google.appengine.api.utils.SystemProperty;


String appId = SystemProperty.applicationId.get();
String appVersion = SystemProperty.applicationVersion.get();

答案 3 :(得分:0)

我在2019年使用Python3尝试了其他方法。据我所知,这些方法适用于Python2(一种适用于Java)。

我能够使用以下命令在Python3中完成此操作

import os

app_id = os.getenv('GAE_APPLICATION')
print(app_id)
project_id = os.getenv('GOOGLE_CLOUD_PROJECT')
print(project_id)

来源:https://cloud.google.com/appengine/docs/standard/python3/runtime

相关问题