7

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"?

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
user2521791
  • 1,621
  • 2
  • 14
  • 13

2 Answers2

6

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)
Pax0r
  • 2,324
  • 2
  • 31
  • 49
  • Your code really made my day, thanks, but I have a question, what is the size of each `chunk`, can I increase the size to speed up the download? – Corey Dec 16 '19 at 06:17
  • 1
    @Corey It has been a while since I done something in the OpenStack, but after taking a qucik look at code I think it's hardcoded here: https://github.com/openstack/python-glanceclient/blob/bf9e6cea186272dda17ad8b874933b2241ee8f97/glanceclient/common/http.py – Pax0r Dec 17 '19 at 08:29
3

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.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Saurabh
  • 76
  • 1