Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openstack python API: how to download image from glance using the python api

I am trying to write a python program to download images from glance service. However, I could not find a way to download images from the cloud using the API. In the documentation which can be found here:

http://docs.openstack.org/user-guide/content/sdk_manage_images.html

they explain how to upload images, but not to download them.

The following code shows how to get image object, but I don't now what to do with this object:

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

is there any way to download the image file and save it on disk using this object "image"?

like image 670
user2521791 Avatar asked May 18 '14 07:05

user2521791


People also ask

Which of the following 2 API are used by glance?

Glance depends on Keystone and the OpenStack Identity API to handle authentication of clients.

What is Python OpenStack API?

OpenStack Python SDK¶ The SDK implements Python bindings to the OpenStack API, which enables you to perform automation tasks in Python by making calls on Python objects, rather than making REST calls directly. All OpenStack command-line tools are implemented using the Python SDK.

What is command to list images in OpenStack?

In the Image service, run the following command: $ openstack image create ISO_IMAGE --file IMAGE.iso \ --disk-format iso --container-format bare. Optionally, to confirm the upload in Image service, run: $ openstack image list.


2 Answers

Without installing glance cli you can download image via HTTP call as described here: http://docs.openstack.org/developer/glance/glanceapi.html#retrieve-raw-image-data

For the python client you can use

img = client.images.get(IMAGE_ID) 

and then call

client.images.data(img) # or img.data()

to retrieve generator by which you can access raw data of image.

Full example (saving image from glance to disk):

img = client.images.find(name='cirros-0.3.2-x86_64-uec')

file_name = "%s.img" % img.name
image_file = open(file_name, 'w+')

for chunk in img.data():
    image_file.write(chunk)
like image 78
Pax0r Avatar answered Oct 05 '22 03:10

Pax0r


You can do this using glance CLI with image-download command:

glance image-download [--file <FILE>] [--progress] <IMAGE>

You will have to install glance cli for this.

Also depending upon the cloud provider/service that you are using, this operation may be disabled for regular user. You might have to check with your provider.

like image 25
Saurabh Avatar answered Oct 05 '22 05:10

Saurabh