如何在不使用终端的 Google App Engine 中获取 locationId
how to get locationId in Google App Engine not using terminal
要在 Python 中使用 scheduler_v1.CloudSchedulerClient().location_path(),我需要具有 projectId 和 LocationId 的父项。我知道如何在代码中获取 projectId 并从终端获取 locationId,但如何在我的代码中获取?
我试过查看这个网站(https://cloud.google.com/functions/docs/reference/rpc/google.cloud.location),但是没有例子,不知道怎么办
from google.cloud import scheduler_v1
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('/home/myname/folder/service_account.json')
service = googleapiclient.discovery.build('cloudresourcemanager', 'v1', credentials = credentials)
request = service.projects().list()
response = request.execute()
client = scheduler_v1.CloudSchedulerClient()
for project in response['projects']:
parent = client.location_path(project['projectId'], ['LocationID'])
for element in client.list_jobs(parent):
print(element)
谢谢!
在Google App Engine 中,有一些环境变量是由运行时环境设置的。
您可以通过 os.environ.get(environment_name).
获取
https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables
不需要使用cloudresourcemanager
获取项目ID,而是可以使用App Engine环境变量GOOGLE_CLOUD_PROJECT
您可以使用 App engine admin API 获取位置 ID,请检查此代码段。
credentials = GoogleCredentials.get_application_default()
#start discovery service and use appengine admin API
service = discovery.build('appengine', 'v1', credentials=credentials, cache_discovery=False)
#disable cache for app engine std (avoid noise in logs)
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
#take the project ID form the environment variables
project = os.environ['GOOGLE_CLOUD_PROJECT']
#get App engine application details
req = service.apps().get(appsId=project)
response =req.execute()
#this is the application location id
location_id = (response["locationId"])
我最近开始使用 http://metadata.google.internal/computeMetadata/v1/instance/region
端点。虽然它在 App Engine 中工作,但现在没有明确记录(但它记录在 Cloud 运行:https://cloud.google.com/run/docs/reference/container-contract)。
该端点的结果将如下所示:
projects/12345678901234/regions/us-central1
很明显,区域是最后一部分(us-central1
)。
这是我在 Go 中的示例(记得将 Metadata-Flavor
header 设置为 Google
):
var region string
{
domain := "http://metadata.google.internal"
path := "computeMetadata/v1/instance/region"
request, err := http.NewRequest(http.MethodGet, domain+"/"+path, nil)
if err != nil {
logrus.Errorf("Could not create request: %v", err)
panic(err)
}
request.Header.Set("Metadata-Flavor", "Google")
response, err := http.DefaultClient.Do(request)
if err != nil {
logrus.Errorf("Could not perform request: %v", err)
panic(err)
}
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
logrus.Errorf("Could not read contents: %v", err)
panic(err)
}
originalRegion := string(contents)
logrus.Infof("Contents of %q: %s", path, originalRegion)
parts := strings.Split(originalRegion, "/")
if len(parts) > 0 {
region = parts[len(parts)-1]
}
}
logrus.Infof("Region: %s", region)
要在 Python 中使用 scheduler_v1.CloudSchedulerClient().location_path(),我需要具有 projectId 和 LocationId 的父项。我知道如何在代码中获取 projectId 并从终端获取 locationId,但如何在我的代码中获取?
我试过查看这个网站(https://cloud.google.com/functions/docs/reference/rpc/google.cloud.location),但是没有例子,不知道怎么办
from google.cloud import scheduler_v1
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('/home/myname/folder/service_account.json')
service = googleapiclient.discovery.build('cloudresourcemanager', 'v1', credentials = credentials)
request = service.projects().list()
response = request.execute()
client = scheduler_v1.CloudSchedulerClient()
for project in response['projects']:
parent = client.location_path(project['projectId'], ['LocationID'])
for element in client.list_jobs(parent):
print(element)
谢谢!
在Google App Engine 中,有一些环境变量是由运行时环境设置的。 您可以通过 os.environ.get(environment_name).
获取https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables
不需要使用cloudresourcemanager
获取项目ID,而是可以使用App Engine环境变量GOOGLE_CLOUD_PROJECT
您可以使用 App engine admin API 获取位置 ID,请检查此代码段。
credentials = GoogleCredentials.get_application_default()
#start discovery service and use appengine admin API
service = discovery.build('appengine', 'v1', credentials=credentials, cache_discovery=False)
#disable cache for app engine std (avoid noise in logs)
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
#take the project ID form the environment variables
project = os.environ['GOOGLE_CLOUD_PROJECT']
#get App engine application details
req = service.apps().get(appsId=project)
response =req.execute()
#this is the application location id
location_id = (response["locationId"])
我最近开始使用 http://metadata.google.internal/computeMetadata/v1/instance/region
端点。虽然它在 App Engine 中工作,但现在没有明确记录(但它记录在 Cloud 运行:https://cloud.google.com/run/docs/reference/container-contract)。
该端点的结果将如下所示:
projects/12345678901234/regions/us-central1
很明显,区域是最后一部分(us-central1
)。
这是我在 Go 中的示例(记得将 Metadata-Flavor
header 设置为 Google
):
var region string
{
domain := "http://metadata.google.internal"
path := "computeMetadata/v1/instance/region"
request, err := http.NewRequest(http.MethodGet, domain+"/"+path, nil)
if err != nil {
logrus.Errorf("Could not create request: %v", err)
panic(err)
}
request.Header.Set("Metadata-Flavor", "Google")
response, err := http.DefaultClient.Do(request)
if err != nil {
logrus.Errorf("Could not perform request: %v", err)
panic(err)
}
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
logrus.Errorf("Could not read contents: %v", err)
panic(err)
}
originalRegion := string(contents)
logrus.Infof("Contents of %q: %s", path, originalRegion)
parts := strings.Split(originalRegion, "/")
if len(parts) > 0 {
region = parts[len(parts)-1]
}
}
logrus.Infof("Region: %s", region)