可以通过以下步骤实现:
from google.cloud import compute_v1
ComputeClient
对象:client = compute_v1.ComputeClient()
project_id = 'your-project-id'
zone = 'your-zone'
instance_name = 'your-instance-name'
client
对象的get
方法获取实例的详细信息:instance = client.get(project=project_id, zone=zone, instance=instance_name)
image_name = instance['disks'][0]['sourceImage'].split('/')[-1]
完整的代码示例:
from google.cloud import compute_v1
client = compute_v1.ComputeClient()
project_id = 'your-project-id'
zone = 'your-zone'
instance_name = 'your-instance-name'
instance = client.get(project=project_id, zone=zone, instance=instance_name)
image_name = instance['disks'][0]['sourceImage'].split('/')[-1]
print('实例镜像名称:', image_name)
这样,你就可以使用gcloud python api获取实例镜像名称了。
领取专属 10元无门槛券
手把手带您无忧上云