gpt4 book ai didi

python - 使用 gcloud python api 获取实例图像名称

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:17 27 4
gpt4 key购买 nike

如何通过 Python 中的 gcloud 计算引擎 API 检索实例的镜像名称?

当我列出我的实例时

compute.instances().list(project=project, zone=zone , filter ='status eq '+ instance_status).execute()

响应没有给我实例镜像名称 (debian-9)。

最佳答案

要使用 Python API 获取源图像,请使用以下请求:

request = service.disks().get(project=project, zone=zone, disk=disk)
response = request.execute()

输出正文有 'sourceImage' 字段,了解更多信息请参阅公众 documentation另请参阅下面的示例:

from pprint import pprint

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('compute', 'v1', credentials=credentials)

# Project ID for this request.
project = 'my-project' # TODO: Update placeholder value.

# The name of the zone for this request.
zone = 'my-zone' # TODO: Update placeholder value.

# Name of the persistent disk to return.
disk = 'my-disk' # TODO: Update placeholder value.

request = service.disks().get(project=project, zone=zone, disk=disk)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)

关于python - 使用 gcloud python api 获取实例图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com