use*_*791 5 python api openstack openstack-glance
我正在尝试编写一个python程序来从一瞥服务下载图像.但是,我找不到使用API从云下载图像的方法.在可在此处找到的文档中:
http://docs.openstack.org/user-guide/content/sdk_manage_images.html
他们解释了如何上传图片,但没有下载图片.
以下代码显示了如何获取图像对象,但我现在不知道如何处理此对象:
import novaclient.v1_1.client as nvclient
name = "cirros"
nova = nvclient.Client(...)
image = nova.images.find(name=name)
Run Code Online (Sandbox Code Playgroud)
有没有办法下载图像文件并使用此对象"图像"将其保存在磁盘上?
无需安装glance cli,您可以通过HTTP调用下载图像,如下所述:http : //docs.openstack.org/developer/glance/glanceapi.html#retrieve-raw-image-data
对于 python 客户端,您可以使用
img = client.images.get(IMAGE_ID)
Run Code Online (Sandbox Code Playgroud)
然后打电话
client.images.data(img) # or img.data()
Run Code Online (Sandbox Code Playgroud)
检索生成器,您可以通过该生成器访问图像的原始数据。
完整示例(将图像从一瞥保存到磁盘):
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)
Run Code Online (Sandbox Code Playgroud)