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

Kev*_* S. 11 python google-app-engine google-bigquery google-cloud-platform

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

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

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

Mih*_*ssu 12

这是"官方"的方式:

from google.appengine.api import app_identity

GAE_APP_ID = app_identity.get_application_id()
Run Code Online (Sandbox Code Playgroud)

点击此处了解详情:https://developers.google.com/appengine/docs/python/appidentity/


Dmy*_*hyi 8

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

import os
print os.getenv('APPLICATION_ID')
print os.getenv('CURRENT_VERSION_ID')
print os.environ
Run Code Online (Sandbox Code Playgroud)


And*_*gin 5

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

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


String appId = SystemProperty.applicationId.get();
String appVersion = SystemProperty.applicationVersion.get();
Run Code Online (Sandbox Code Playgroud)


小智 5

我在 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)
Run Code Online (Sandbox Code Playgroud)

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