首页 > 解决方案 > 如何获取已部署 App Engine 项目的 URL

问题描述

我已经使用 maven ( https://cloud.google.com/appengine/docs/flexible/java/using-maven )在 App Engine 中部署了我的应用程序

如何以编程方式获取 APP 的 URL?我知道它遵循这种格式https://PROJECT_ID.REGION_ID.r.appspot.com。但是,我不知道区域 ID。如果没有办法获取URL,有没有办法获取区域ID?

编辑

我发现这对此很有用: https ://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps/get

标签: javamavengoogle-app-engine

解决方案


我在我的生产环境中使用此代码来识别 URL。目前无法使用代码动态获取REGIONID 。至少在我当前的 SDK 中它不可用。总有一天他们会那样做。

根据 Google 文档,目前REGIONID.r在 URL 中不是强制性的。在未来的项目中可能需要它。此代码将为您工作。 https://cloud.google.com/appengine/docs/standard/python/how-requests-are-routed#region-id

import com.google.appengine.api.modules.ModulesServiceFactory;
import com.google.appengine.api.utils.SystemProperty;
import com.google.apphosting.api.ApiProxy;

public static void main(String[] args) {

    String projectId = ApiProxy.getCurrentEnvironment().getAppId().indexOf("~")>-1?
                ApiProxy.getCurrentEnvironment().getAppId().split("~")[1]:ApiProxy.getCurrentEnvironment().getAppId();
    ModulesService ssf = ModulesServiceFactory.getModulesService();
    String url = null;
    if(SystemProperty.environment.value()!=SystemProperty.Environment.Value.Production) {
        url = "http://localhost:PORT_NUMBER";
    }
    if(ssf.getDefaultVersion(ssf.getCurrentModule()).equals(ssf.getCurrentVersion())) {
        //This is default version
        url = "https://" + projectId + ".appspot.com";
    }
    //The URL with the current version ID  
    url = "https://" + ssf.getCurrentVersion() + "-dot-" + projectId + ".appspot.com";
    //The URL with the module name, current version name
    url = "https://" + ssf.getCurrentVersion() + "-dot-" + ssf.getCurrentModule() + "-dot-" + projectId + ".appspot.com";

}

推荐阅读