2014-11-25 116 views
0

我在openstacks的对象存储中上传了一些文件。该片段提取容器和对象列表并打印。解析来自Swift api的Json响应时的异常

private void listContainers() { 
     ContainerApi containerApi = swiftApi.getContainerApiForRegion("region"); 
     Set<Container> containers = containerApi.list().toSet(); 

     for (Container container : containers) { 
      ObjectApi objectApi = swiftApi.getObjectApiForRegionAndContainer("region", container.getName()); 

      ObjectList objects = objectApi.list(); // crashes here 
      for (SwiftObject object: objects) { 
      System.out.println("\t\t"+ object); 
      } 

      System.out.println("\t" + container); 
     } 
    } 

控制台输出:

DEBUG o.j.rest.internal.InvokeHttpMethod - >> invoking container:list 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request -363056976: GET http://127.0.0.1/swift/v1/?format=json HTTP/1.1 
DEBUG jclouds.headers - >> GET http://127.0.0.1/swift/v1/?format=json HTTP/1.1 
DEBUG jclouds.headers - >> Accept: application/json 
DEBUG jclouds.headers - >> X-Auth-Token: MIIQ... 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response -363056976: HTTP/1.1 200 OK 
DEBUG jclouds.headers - << HTTP/1.1 200 OK 
DEBUG jclouds.headers - << Transfer-Encoding: chunked 
DEBUG jclouds.headers - << Date: Tue, 25 Nov 2014 07:39:44 GMT 
DEBUG jclouds.headers - << Keep-Alive: timeout=5, max=98 
DEBUG jclouds.headers - << Connection: Keep-Alive 
DEBUG jclouds.headers - << Server: Apache/2.2.22 (Ubuntu) 
DEBUG jclouds.headers - << Content-Type: application/json; charset=utf-8 
DEBUG jclouds.wire - << "[{"name":"jclouds-example","count":1,"bytes":12},{"name":"test_name","count":3,"bytes":22008217}]" 
DEBUG o.j.rest.internal.InvokeHttpMethod - >> invoking object:list 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request -1472717862: GET http://127.0.0.1/swift/v1/jclouds-example/?format=json HTTP/1.1 
DEBUG jclouds.headers - >> GET http://127.0.0.1/swift/v1/jclouds-example/?format=json HTTP/1.1 
DEBUG jclouds.headers - >> Accept: application/json 
DEBUG jclouds.headers - >> X-Auth-Token: MIIQ... 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response -1472717862: HTTP/1.1 200 OK 
DEBUG jclouds.headers - << HTTP/1.1 200 OK 
DEBUG jclouds.headers - << Transfer-Encoding: chunked 
DEBUG jclouds.headers - << Date: Tue, 25 Nov 2014 07:39:44 GMT 
DEBUG jclouds.headers - << Keep-Alive: timeout=5, max=97 
DEBUG jclouds.headers - << Connection: Keep-Alive 
DEBUG jclouds.headers - << Server: Apache/2.2.22 (Ubuntu) 
DEBUG jclouds.headers - << Content-Type: application/json; charset=utf-8 
DEBUG jclouds.wire - << "[{"name":"jclouds-example.txt","hash":"ed076287532e86365e841e92bfc50d8c","bytes":12,"content_type":"application\/unknown","last_modified":"2014-11-25T07:39:44.000Z"}]" 
java.lang.NumberFormatException: null 
    at java.lang.Long.parseLong(Long.java:404) 
    at java.lang.Long.parseLong(Long.java:483) 
    at org.jclouds.openstack.swift.v1.functions.ParseContainerFromHeaders.apply(ParseContainerFromHeaders.java:39) 
    at org.jclouds.openstack.swift.v1.functions.ParseObjectListFromResponse.apply(ParseObjectListFromResponse.java:66) 
    at org.jclouds.openstack.swift.v1.functions.ParseObjectListFromResponse.apply(ParseObjectListFromResponse.java:41) 
    at org.jclouds.rest.internal.InvokeHttpMethod.invoke(InvokeHttpMethod.java:90) 
    at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:73) 
    at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:44) 
    at org.jclouds.reflect.FunctionalReflection$FunctionalInvocationHandler.handleInvocation(FunctionalReflection.java:117) 
    at com.google.common.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:87) 
    at com.sun.proxy.$Proxy66.list(Unknown Source) 
    at test.jcloud.JCloudsSwift.listContainers(JCloudsSwift.java:99) 
    at test.jcloud.JCloudsSwift.main(JCloudsSwift.java:39) 

如果我指定的文件名,并获取其元数据,一切正常:

... 
for (Container container : containers) { 
    ObjectApi objectApi = swiftApi.getObjectApiForRegionAndContainer("region", container.getName()); 
    SwiftObject obj = objectApi.get("jclouds-example.txt"); 
    System.out.println("-- "+obj.getMetadata()); 
    ObjectList objects = objectApi.list(); 
... 

控制台输出:

DEBUG o.j.rest.internal.InvokeHttpMethod - >> invoking object:get 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request -614124758: GET http://127.0.0.1/swift/v1/jclouds-example/jclouds-example.txt HTTP/1.1 
DEBUG jclouds.headers - >> GET http://127.0.0.1/swift/v1/jclouds-example/jclouds-example.txt HTTP/1.1 
DEBUG jclouds.headers - >> Accept: application/json 
DEBUG jclouds.headers - >> X-Auth-Token: MIIQ... 
DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response -614124758: HTTP/1.1 200 OK 
DEBUG jclouds.headers - << HTTP/1.1 200 OK 
DEBUG jclouds.headers - << etag: ed076287532e86365e841e92bfc50d8c 
DEBUG jclouds.headers - << Date: Tue, 25 Nov 2014 07:48:49 GMT 
DEBUG jclouds.headers - << Last-Modified: Tue, 25 Nov 2014 07:48:49 GMT 
DEBUG jclouds.headers - << Keep-Alive: timeout=5, max=97 
DEBUG jclouds.headers - << X-Object-Meta-key4: blu 
DEBUG jclouds.headers - << X-Object-Meta-key3: value3 
DEBUG jclouds.headers - << Connection: Keep-Alive 
DEBUG jclouds.headers - << Accept-Ranges: bytes 
DEBUG jclouds.headers - << Server: Apache/2.2.22 (Ubuntu) 
DEBUG jclouds.headers - << Content-Type: application/unknown 
DEBUG jclouds.headers - << Content-Length: 12 
DEBUG jclouds.wire - << "Hello World!" 
-- {key4=blu, key3=value3} 

的代码基于this例子和this例如

问题

获取物体的信息,我得到NumberFormatException时jclouds转换包含对象列表,java对象JSON数据。

问题

我怎样才能得到文件列表中为每个容器,并得到他们的元数据?

回答

1

我刚刚在OpenStack Juno上用jclouds 1.8.1试过这个,它对我很有用。你使用的是什么版本的OpenStack和jcloud?

我还注意到对象列表响应缺少X-Container-Object-CountX-Container-Bytes-Used标题。这很奇怪,他们应该在那里根据这个api reference。您的Apache服务器可能是操纵或过滤标头?

如果你想尝试jclouds 1.8.1,你首先需要阅读这些release notes

+0

我使用jclouds 1.8.1和OpenStack的2014.1 – gkiko 2014-12-04 06:59:02

+0

斯威夫特真的应该重新调整的X-集装箱-对象计数和X-集装箱字节,用于头,他们已经出现自很久以前2014.1(见[ server.py](https://github.com/openstack/swift/blob/fbc58b082cf93eb142c038f24e75ec01e6005894/swift/container/server.py#L62-70))。您的Apache服务器可能是操纵或过滤标头? – 2014-12-04 20:36:16

+0

另外,你是如何得到以?format = json'GET http://127.0.0.1/swift/v1/jclouds-example/?format = json'结尾的请求?在1.8.1中调用ObjectApi.list()时无法做到这一点(请参见[ObjectApi.java](https://github.com/jclouds/jclouds/blob/master/apis/openstack-swift/src/main /java/org/jclouds/openstack/swift/v1/features/ObjectApi.java#L79-84))。 – 2014-12-04 20:39:55