【问题标题】:Google Compute Engine Python API and web API do not return images or kernelsGoogle Compute Engine Python API 和 Web API 不返回图像或内核
【发布时间】:2013-08-20 02:28:45
【问题描述】:

在运行 Google Compute Engine 时,在查询图像和内核时,我得到了与 gcutil 不同的意外结果。值得注意的是,我没有得到任何结果。

gcutil 我得到了预期的结果:

$ gcutil --project MYPROJECT 列表图像 +-------------------------------------------------- -------------+------------------------------------------------ ------------------------------+------------------ ---------------+-------------+-------- + |姓名 |描述 |内核 |弃用 |状态 | +-------------------------------------------------- -------------+------------------------------------------------ ------------------------------+------------------ ---------------+-------------+-------- + |项目/centos-cloud/global/images/centos-6-v20130731 |支持 SCSI 的 CentOS 6;创建于 2013 年 7 月 31 日星期三 00:00:00 +0000 |项目/谷歌/全球/内核/gce-v20130603 | |准备好了 | |项目/debian-cloud/global/images/debian-6-squeeze-v20130723 | Debian GNU/Linux 6.0.7 (squeeze) 建于 2013-07-23 |项目/谷歌/全球/内核/gce-v20130603 | |准备好了 | |项目/debian-cloud/global/images/debian-7-wheezy-v20130723 | Debian GNU/Linux 7.1 (wheezy) 建立于 2013-07-23 |项目/谷歌/全球/内核/gce-v20130603 | |准备好了 | +-------------------------------------------------- -------------+------------------------------------------------ ------------------------------+------------------ ---------------+-------------+-------- +

但是当我使用 web API 或 Python API 时(缩写,但基本上直接来自 examples):

def _service():
    flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=GCE_SCOPE)
    storage = Storage(OAUTH2_STORAGE)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = run(flow, storage)
    http = httplib2.Http()
    auth_http = credentials.authorize(http)
    gce_service = build('compute', API_VERSION)
    return gce_service, credentials, auth_http

if __name__ == "__main__":
    gce_service, credentials, auth_http = _service()
    request = gce_service.images().list(project="MYPROJECT", filter=None)
    response = request.execute(http=auth_http)
    pprint(response)

Web 和 Python API 都返回如下内容:

{ “种类”:“计算#imageList”, "selfLink": "https://www.googleapis.com/compute/v1beta15/projects/MYPROJECT/global/images", “id”:“项目/MYPROJECt/global/images” }

即缺少itemsone expects in the response

我在这里可能做错了什么?

【问题讨论】:

    标签: python google-compute-engine


    【解决方案1】:

    谷歌(和公众)提供的图像和内核存储在不同的项目中。这个想法是,它们本身并不是你的,而是由 google(或 debian 或 centos)发布的。

    您可以使用gcutil listkernels --project=google 列出内核。与代码等价的是将MYPROJECT替换为google

    对于图像,您可以使用 centos-cloud 用于 CentOS 图像,debian-cloud 用于 debian 图像。

    gcutil 自动从常用项目集成以方便使用。

    【讨论】:

    • 非常明智,非常感谢。希望我在文档中注意到这一点。乔,你知道在哪里可以找到这些信息和类似信息吗?
    【解决方案2】:

    来自 Google https://developers.google.com/compute/docs/api/python_guide#listinginstances 的示例应用正确列出了图像。 不确定代码是如何工作的。您用一个参数定义函数 _service(opts),然后在第 13 行调用它,不带参数 gce_service, credentials, auth_http = _service()

    请提供工作代码。

    【讨论】:

    • 谢谢尤里。我通过将opts 参数删除到_service 来修复代码。您可能需要注意,在这里列出实例不是问题;将 gce_service.images 更改为 gce_service.instancesgce_service.zones 将产生预期的结果 - 实例列表。问题在于内核和图像。
    • 布莱恩,很抱歉。因此,我在 Google 的示例中更改了 2 行
    • 我在 Google 的示例中更改了 2 行,现在得到了 图像 列表。我用我的实际项目 ID 替换了代码中的表达式 project="MYPROJECT"。运行它我还获得了图像列表。所以看起来问题不在API或python库中。
    • 谢谢尤里。我将研究身份验证等问题,并在此处发布我的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多