java - 如何获取已部署 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
解决方案
我在我的生产环境中使用此代码来识别 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";
}
推荐阅读
- javascript - 尝试在表单数据中传递关联数组的数组
- excel - EXCEL - 如果列中的单元格包含列表中的任何字符串(特定于每一行的列表),则取平均值
- python - 使用 pipreqs 和 freeze 创建 requirements.txt
- microsoft-graph-api - Graph API - 带有图像的文件搜索文件夹
- excel - 需要excel的代码来根据另一列中的值变化来填充列中的时间,但我需要在2个不同的列中
- go - 如何在 golang 中导入生成的 SDK?
- regex - 正则表达式从字符串中提取一些特定的数字,尝试了几种方法
- javascript - RegExp 仅匹配捕获组中的前两个条目(无论它们是什么)
- flutter - 在颤动手势检测器中,有没有办法获得增量?
- php - Auth::validate 返回错误 Laravel 8