从Swift api解析Json响应时出现异常

时间:2014-11-25 07:57:38

标签: jclouds openstack-swift

我在openstacks中上传了一些文件'对象商店。此代码段提取容器和对象列表并打印。

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)

如果我指定文件名并获取其&#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示例

上获取对象信息

问题

当jclouds将包含对象列表的json数据转换为java对象时,我得到NumberFormatException

问题

如何获取每个容器的文件列表并获取其元数据?

1 个答案:

答案 0 :(得分: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