Manage images

Manage images

When working with images in the SDK, you will call bothglance and nova methods.

用SDK使用 image ,需要调用glance和nova的方法。

List images

To list the available images, call the glanceclient.v2.images.Controller.list method:

glanceclient.v2.images.Controller.list 调用此方法,列出镜像。此方法将返回一个生成器: <generator object list at 0x105e9c2d0>。

import glanceclient.v2.client as glclient
glance = glclient.Client(...)
images = glance.images.list()

Get image by ID

To retrieve an image object from its ID, call the glanceclient.v2.images.Controller.get method:

用镜像ID返回镜像对象,通过调用 glanceclient.v2.images.Controller.get 方法。

import glanceclient.v2.client as glclient
image_id = 'c002c82e-2cfa-4952-8461-2095b69c18a6'
glance = glclient.Client(...)
image = glance.images.get(image_id)

Get image by name

The Image service Python bindings do not support the retrieval of an image object by name. However, the Compute Python bindings enable you to get an image object by name. To get an image object by name, call thenovaclient.v1_1.images.ImageManager.find method:

Image服务不支持通过名字检索镜像对象,但是Compute支持此项功能,可以通过novaclient.v1_1.images.ImageManager.find 方法通过名称得到镜像。

import novaclient.v1_1.client as nvclient
name = "cirros"
nova = nvclient.Client(...)
image = nova.images.find(name=name)

Upload an image

To upload an image, call the glanceclient.v2.images.ImageManager.create method:

上传镜像可以通过调用 glanceclient.v2.images.ImageManager.create方法。

import glanceclient.v2.client as glclient
imagefile = "/tmp/myimage.img" #上传镜像的路径
glance = glclient.Client(...)
with open(imagefile) as fimage:
  glance.images.create(name="myimage", is_public=False, disk_format="qcow2",
                       container_format="bare", data=fimage)