【问题标题】:Determine what project id my App Engine code is running on确定我的 App Engine 代码在哪个项目 ID 上运行
【发布时间】:2014-10-22 13:05:47
【问题描述】:

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

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

编辑:忘了说这是来自 Python

【问题讨论】:

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


    【解决方案1】:

    这是“官方”方式:

    from google.appengine.api import app_identity
    
    GAE_APP_ID = app_identity.get_application_id()
    

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

    【讨论】:

      【解决方案2】:

      你可以从环境变量中得到很多信息:

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

      【讨论】:

        【解决方案3】:

        我在 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

        【讨论】:

          【解决方案4】:

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

          import com.google.appengine.api.utils.SystemProperty;
          
          
          String appId = SystemProperty.applicationId.get();
          String appVersion = SystemProperty.applicationVersion.get();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-02
            • 2020-08-02
            • 1970-01-01
            相关资源
            最近更新 更多