我需要在谷歌云上寻求帮助,我正在使用谷歌云开发一个应用程序。在谷歌云上我有一个Windows实例和安装了谷歌云SDK的实例。我需要一个命令来返回该实例所在的区域名称。 注意: 我不需要区域列表。 我只需要我的实例所在的区域名称。 谢谢您的帮助。
gcloud compute instances list <your instance name> --format 'csv[no-heading](zone)'
这个操作同时完成了两件事情。它
gcloud compute instances list your-instance-name
该部分列出了所有具有该名称的实例,例如:
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
your-instance-name europe-west1-c n1-standard-1 1.000.000.001 100.000.000.01 RUNNING
而该
--format 'csv[no-heading](zone)'
zone
列。有关格式化输出的更多信息,请参见https://cloud.google.com/sdk/gcloud/reference/topic/formats(或gcloud help topic formats
)。gcloud compute instances list --filter="name=('`hostname`')" --format 'csv[no-heading](zone)'
如果您更喜欢使用GoogleCloud PowerShell模块,以下内容将帮助您获取区域的URI:
$zone = (Get-GCEInstance).Where({$_.Name -eq $(hostname)}).Zone
https://www.googleapis.com/compute/v1/projects/my-project-001/zones/us-east1-a
$zone.Substring($zone.LastIndexOf("/")+1)
返回:
us-east1-a
curl http://metadata.google.internal/computeMetadata/v1/instance/zone -H Metadata-Flavor:Google | cut '-d/' -f4
gcloud compute instances list --filter="name=('your-hostname')" --format "value(zone)"
。建议改为:gcloud compute instances list --filter="name=('your-hostname')" --format="value(zone)"
。 - xmedeko--format "value(zone)"
非常有用。 - Dee