Create images with OpenStack Rest API

Official API documentation https://developer.openstack.org/api-ref/image/v2/#upload-binary-image-data


Create first and then upload

Service endpoint:

    image

API:

    POST     /v2/images

Request1:

    Method:Post

    Url: http://192.168.31.147:9292/v2/images

    Headers:

Content-Type: application/json
X-Auth-Token: 4e045ef71a994f8d8caeeab683a11419

    Body:

{
    "name": "registry:5000/centos:v1.0",
    "container_format": "docker",
    "disk_format": "raw"
}

Answer1:

    Headers:

Status Code: 201 

    Body:

copy code
{
    "status": "queued",
    "name": "registry:5000/centos:v1.0",
    "tags": [],
    "container_format": "docker",
    "created_at": "2015-05-22T06:38:10Z",
    "disk_format": "raw",
    "updated_at": "2015-05-22T06:38:10Z",
    "visibility": "private",
    "self": "/v2/images/0482eff2-8406-46a7-8bbb-d43e1ca9c14f",
    "min_disk": 0,
    "protected": false,
    "id": "0482eff2-8406-46a7-8bbb-d43e1ca9c14f",
    "file": "/v2/images/0482eff2-8406-46a7-8bbb-d43e1ca9c14f/file",
    "owner": "67f095375f414c3b8fde844a5e08b88e",
    "min_ram": 0,
    "schema": "/v2/schemas/image"
}
copy code

 

Request2:

$ curl -i -X PUT -H "X-Auth-Token: 94f2b7bf4cee4f2c9d2bbe1f2ca785a2" -H "Content-Type: application/octet-stream"  --upload-file /home/centos.tar http://192.168.31.147:9292/v2/images/0482eff2-8406-46a7-8bbb-d43e1ca9c14f/file

Reponse2:

    Headers:

copy code
HTTP/1.1 100 Continue

HTTP/1.1 204 No Content
Content-Type: text/html; charset=UTF-8
Content-Length: 0
X-Openstack-Request-Id: req-98582037-dca6-4792-8d4a-a76d04464834
Date: Fri, 22 May 2015 06:38:47 GMT
copy code

 

    个人感想:Openstack官网对创建镜像的API描述真心的少啊,而且只有第一步,就是上文提到的request1。request2是笔者废了九牛二虎之力才找到的,且一开始数据源的上传用的是 -d @/home/centos.tar  这个选项,但是经过反复试验,这个选项TM上传文件不完整,800M的文件只能上传230M,反复查找问题也找不到,最终换成--upload-file选项,上传成功,且根据此镜像可正常启动实例。

Example call:

curl -i -X PUT -H "X-Auth-Token: $token" \
   -H "Content-Type: application/octet-stream" \
   -d @/home/glance/ubuntu-12.10.qcow2 \
   $image_url/v2/images/{image_id}/file


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326203130&siteId=291194637