openstack 通过 java api 上传image

//--------------上传glance 信息
    public static int UpLoadGlanceImage() {
        File f = new File(    "/home/lost/download/precise-server-cloudimg-amd64-disk1.img");
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            byte b[] = new byte[(int) f.length()]; // 创建合适文件大小的数组
            in.read(b); // 读取文件中的内容到b[]数组
            
              ImageForUpload  imageUpload = new ImageForUpload();
              imageUpload.setContainerFormat("ovf");
              imageUpload.setDiskFormat("qcow2");
              imageUpload.setName("ubuntu12.04_TestForUpload");
              imageUpload.setOwner("admin");
              imageUpload.setPublic(true);
              imageUpload.setInputStream(in);
              glanceClient.execute(new UploadImage(imageUpload));
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
          return 0;

      }



public static void Init() {
        keystone = new KeystoneClient(KEYSTONE_AUTH_URL);
        AbstractOpenStackClient client = new AbstractOpenStackClient(
                KEYSTONE_AUTH_URL);
        access = client
                .request("/tokens")
                .execute(
                        "POST",
                        Entity.json("{\"auth\":{\"passwordCredentials\":{\"username\":\""
                                + KEYSTONE_USERNAME
                                + "\",\"password\":\""
                                + KEYSTONE_PASSWORD
                                + "\"},\"tenantId\":\""
                                + KEYSTONE_TOKEN + "\"}}"), Access.class);
        tokenTemp = access.getToken().getId();
        if (tokenTemp != null) {
            keystone.setToken(tokenTemp); // 获取access中 id [token=Token                                        
        }
        tenants = keystone.execute(new ListTenants());// 这个是获取keystoen数据库表中tenant表数据                                            
        if (tenants.getList().size() > 0) {
            authentication = new Authentication();
            Token token = new Token();
            token.setId(tokenTemp);
            authentication.setToken(token); // 就是上面获取的id:95d304ab72a14933be27ec2601355146
            authentication.setTenantId(tenants.getList().get(0).getId());
            access = keystone.execute(new Authenticate(authentication));
            //System.out.println(access);
            glanceClient = new GlanceClient(KeystoneUtils.findEndpointURL(
                    access.getServiceCatalog(), "compute", null, "public"),
                    tokenTemp);
        }else {
            System.out.println("No tenants found!");
        }
    }

   
    public static void main(String[] args) {
        Init();
        ListGlanceimage();
        UpLoadGlanceImage();
        
        
    }
   
异常:

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * LoggingFilter - Request received on thread main
1 > POST http://192.168.120.228:5000/v2.0/tokens
1 > Accept: application/json
1 > Content-Type: application/json
{"auth":{"passwordCredentials":{"username":"glance","password":"admin"},"tenantId":"4ad16e2e8b54476095558354a6623380"}}

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * LoggingFilter - Response received on thread main
2 < 200
2 < Transfer-Encoding: chunked
2 < Date: Tue, 18 Dec 2012 13:25:59 GMT
2 < Vary: X-Auth-Token
2 < Connection: keep-alive
2 < Content-Type: application/json
{"access": {"token": {"expires": "2012-12-19T13:25:59Z", "id": "4e9f1bf98fec4bf59e22b6b5642eec14", "tenant": {"description": null, "enabled": true, "id": "4ad16e2e8b54476095558354a6623380", "name": "service"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380", "region": "RegionOne", "internalURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://192.168.120.228:9292/v1", "region": "RegionOne", "internalURL": "http://192.168.120.228:9292/v1", "publicURL": "http://192.168.120.228:9292/v1"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380", "region": "RegionOne", "internalURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "volume", "name": "volume"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8773/services/Admin", "region": "RegionOne", "internalURL": "http://192.168.120.228:8773/services/Cloud", "publicURL": "http://192.168.120.228:8773/services/Cloud"}], "endpoints_links": [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8080/v1", "region": "RegionOne", "internalURL": "http://192.168.120.228:8080/v1/AUTH_4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8080/v1/AUTH_4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "object-store", "name": "swift"}, {"endpoints": [{"adminURL": "http://192.168.120.228:35357/v2.0", "region": "RegionOne", "internalURL": "http://192.168.120.228:5000/v2.0", "publicURL": "http://192.168.120.228:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "glance", "roles_links": [], "id": "e1c652e001fe4abc819bdae3d0f36bb7", "roles": [{"id": "009117b398dd4b229d36de75b68210fb", "name": "admin"}], "name": "glance"}}}

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 3 * LoggingFilter - Request received on thread main
3 > GET http://192.168.120.228:5000/v2.0/tenants
3 > Accept: application/json
3 > X-Auth-Token: 4e9f1bf98fec4bf59e22b6b5642eec14

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 4 * LoggingFilter - Response received on thread main
4 < 200
4 < Transfer-Encoding: chunked
4 < Date: Tue, 18 Dec 2012 13:25:59 GMT
4 < Vary: X-Auth-Token
4 < Connection: keep-alive
4 < Content-Type: application/json
{"tenants_links": [], "tenants": [{"enabled": true, "description": null, "name": "service", "id": "4ad16e2e8b54476095558354a6623380"}]}

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 5 * LoggingFilter - Request received on thread main
5 > POST http://192.168.120.228:5000/v2.0/tokens
5 > Accept: application/json
5 > Content-Type: application/json
5 > X-Auth-Token: 4e9f1bf98fec4bf59e22b6b5642eec14
{
  "auth" : {
    "tenantId" : "4ad16e2e8b54476095558354a6623380",
    "token" : {
      "id" : "4e9f1bf98fec4bf59e22b6b5642eec14"
    }
  }
}

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * LoggingFilter - Response received on thread main
6 < 200
6 < Transfer-Encoding: chunked
6 < Date: Tue, 18 Dec 2012 13:25:59 GMT
6 < Vary: X-Auth-Token
6 < Connection: keep-alive
6 < Content-Type: application/json
{"access": {"token": {"expires": "2012-12-19T13:25:59Z", "id": "b423c96b75344384ad71dac5c032fe8d", "tenant": {"description": null, "enabled": true, "id": "4ad16e2e8b54476095558354a6623380", "name": "service"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380", "region": "RegionOne", "internalURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://192.168.120.228:9292/v1", "region": "RegionOne", "internalURL": "http://192.168.120.228:9292/v1", "publicURL": "http://192.168.120.228:9292/v1"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380", "region": "RegionOne", "internalURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8776/v1/4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "volume", "name": "volume"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8773/services/Admin", "region": "RegionOne", "internalURL": "http://192.168.120.228:8773/services/Cloud", "publicURL": "http://192.168.120.228:8773/services/Cloud"}], "endpoints_links": [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://192.168.120.228:8080/v1", "region": "RegionOne", "internalURL": "http://192.168.120.228:8080/v1/AUTH_4ad16e2e8b54476095558354a6623380", "publicURL": "http://192.168.120.228:8080/v1/AUTH_4ad16e2e8b54476095558354a6623380"}], "endpoints_links": [], "type": "object-store", "name": "swift"}, {"endpoints": [{"adminURL": "http://192.168.120.228:35357/v2.0", "region": "RegionOne", "internalURL": "http://192.168.120.228:5000/v2.0", "publicURL": "http://192.168.120.228:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "glance", "roles_links": [], "id": "e1c652e001fe4abc819bdae3d0f36bb7", "roles": [{"id": "009117b398dd4b229d36de75b68210fb", "name": "admin"}], "name": "glance"}}}

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 7 * LoggingFilter - Request received on thread main
7 > GET http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380/images
7 > Accept: application/json
7 > X-Auth-Token: 4e9f1bf98fec4bf59e22b6b5642eec14

十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 8 * LoggingFilter - Response received on thread main
8 < 200
8 < Date: Tue, 18 Dec 2012 13:25:59 GMT
8 < Content-Length: 548
8 < Connection: keep-alive
8 < Content-Type: application/json
8 < X-Compute-Request-Id: req-8c161f1a-1c96-4b2d-bea0-b15a3c23239d
{"images": [{"id": "f60c6377-b5cb-4700-92c1-f5b7ab4dd377", "links": [{"href": "http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380/images/f60c6377-b5cb-4700-92c1-f5b7ab4dd377", "rel": "self"}, {"href": "http://192.168.120.228:8774/4ad16e2e8b54476095558354a6623380/images/f60c6377-b5cb-4700-92c1-f5b7ab4dd377", "rel": "bookmark"}, {"href": "http://192.168.120.228:9292/4ad16e2e8b54476095558354a6623380/images/f60c6377-b5cb-4700-92c1-f5b7ab4dd377", "type": "application/vnd.openstack.image", "rel": "alternate"}], "name": "ubuntu12.04"}]}

class org.openstack.glance.model.Image   Image [id=f60c6377-b5cb-4700-92c1-f5b7ab4dd377, uri=null, name=ubuntu12.04, diskFormat=null, containerFormat=null, size=null, checksum=null, createdAt=null, updatedAt=null, deletedAt=null, status=null, isPublic=false, minRam=null, minDisk=null, owner=null, isDeleted=false, isProtected=false, properties=null]
十二月 18, 2012 9:25:59 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 9 * LoggingFilter - Request received on thread main
9 > POST http://192.168.120.228:8774/v2/4ad16e2e8b54476095558354a6623380/images
9 > Accept: application/json
9 > x-image-meta-name: ubuntu12.04_TestForUpload
9 > x-image-meta-disk_format: qcow2
9 > x-image-meta-container_format: ovf
9 > x-image-meta-is-public: true
9 > x-image-meta-owner: 4ad16e2e8b54476095558354a6623380
9 > Content-Type: application/octet-stream
9 > X-Auth-Token: 4e9f1bf98fec4bf59e22b6b5642eec14


十二月 18, 2012 9:26:00 下午 org.glassfish.jersey.filter.LoggingFilter log
INFO: 10 * LoggingFilter - Response received on thread main
10 < 401
10 < Date: Tue, 18 Dec 2012 13:26:00 GMT
10 < Content-Length: 381
10 < Www-Authenticate: Keystone uri='http://127.0.0.1:5000/'
10 < Connection: keep-alive
10 < Content-Type: text/html; charset=UTF-8
<html>
 <head>
  <title>401 Unauthorized</title>
 </head>
 <body>
  <h1>401 Unauthorized</h1>
  This server could not verify that you are authorized to access the document you requested. Either you supplied the wrong credentials (e.g., bad password), or your browser does not understand how to supply the credentials required.<br /><br />
Authentication required


 </body>
</html>

javax.ws.rs.NotAuthorizedException
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:850)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:728)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:86)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:629)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:420)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:625)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:387)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:290)
    at org.openstack.glance.api.UploadImage.execute(UploadImage.java:54)
    at org.openstack.glance.api.UploadImage.execute(UploadImage.java:12)
    at org.openstack.glance.GlanceClient.execute(GlanceClient.java:19)
    at edu.hnu.lost.openstack.glance.test.TestOpenstackGlance01.UpLoadGlanceImage(TestOpenstackGlance01.java:126)
    at edu.hnu.lost.openstack.glance.test.TestOpenstackGlance01.main(TestOpenstackGlance01.java:51)


不知道为什么  通过这个方式 我可以list a image 和show(image_id)   image

but   upload image  failure  ??????





评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值