Ask Your Question
0

python failed image upload

asked 2016-01-30 11:35:16 -0600

smokes2345 gravatar image

I have the following code:

    print "Uploading image from {0}".format(img_file)
    with open(img_file,'rb') as handle:
        try:
            self.dst_gl.images.create(data=handle,name=img_name,disk_format=self.src_img.disk_format,container_format=self.src_img.container_format)
        except Exception as e:
            print e

That throws the following error:

    Unable to set 'data' to '<open file '/tmp/matdaivd-migration-test_rootvol.raw', mode 'rb' at        0x7fb0c44856f0>'. Reason: <open file '/tmp/matdaivd-migration-test_rootvol.raw', mode 'rb' at       0x7fb0c44856f0> is not of type u'string'

    Failed validating u'type' in schema[u'additionalProperties']:
        {u'type': u'string'}

"self.dst_gl.images" is an object of type glanceclient.v2.images.ImageManager. From what I understand OS is basically saying that the object type for the image data is incorrect, but i've followed the example almost exactly. Can anyone provide any insight as to how I might get around this error?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-02-01 11:02:45 -0600

dbaxps gravatar image

updated 2016-02-01 11:06:30 -0600

This code works for me
[root@ip-192-169-142-127 ~(keystone_admin)]# cat uploadGlance.py

#!/usr/bin/env  python
import keystoneclient.v2_0.client as ksclient
import glanceclient
import os

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

creds = get_keystone_creds()
keystone = ksclient.Client(**creds)
glance_endpoint = keystone.service_catalog.url_for(service_type='image',
                                                   endpoint_type='publicURL')
glance = glanceclient.Client('1',glance_endpoint, token=keystone.auth_token)
with open('./ubuntu-15.10-server-cloudimg-amd64-disk1.img') as fimage:
    glance.images.create(name="Ubuntu15.10Cloud-via-python", is_public=True, disk_format="qcow2",
                         container_format="bare", data=fimage)

[root@ip-192-169-142-127 ~(keystone_admin)]# glance image-list

+--------------------------------------+-----------------------+
| ID                                   | Name                  |
+--------------------------------------+-----------------------+
| b5d502f1-274b-489b-bcf1-a4975c072706 | cirros                |
| 8d71b324-09a4-4797-bbba-66d7e09aa213 | Ubuntu1404Cloud       |
| 60d9f250-74b3-48ea-982e-46611a231b63 | Ubuntu1510Cloud-image |
| c8f4dcf2-e4cd-4541-aadb-84604e7bf3da | VF23Cloud             |
+--------------------------------------+-----------------------+

[root@ip-192-169-142-127 ~(keystone_admin)]# python uploadGlance.pyc

[root@ip-192-169-142-127 ~(keystone_admin)]# glance image-list
+--------------------------------------+-----------------------------+
| ID                                   | Name                        |
+--------------------------------------+-----------------------------+
| b5d502f1-274b-489b-bcf1-a4975c072706 | cirros                      |
| 8d71b324-09a4-4797-bbba-66d7e09aa213 | Ubuntu1404Cloud             |
| 7a214e14-579d-463c-9441-b334b7d90480 | Ubuntu15.10Cloud-via-python |
| 60d9f250-74b3-48ea-982e-46611a231b63 | Ubuntu1510Cloud-image       |
| c8f4dcf2-e4cd-4541-aadb-84604e7bf3da | VF23Cloud                   |
+--------------------------------------+-----------------------------+
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2016-01-30 09:30:16 -0600

Seen: 376 times

Last updated: Feb 01 '16