我正在尝试使用Apache JClouds 1.9.2 库访问OpenStack Swift 。Maven依赖:
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-all</artifactId>
<version>1.9.2</version>
</dependency>
我从入门指南及其代码示例开始。一般来说,连接和持久化 blob 没有问题。但是当我尝试列出容器列表(或容器的内容)时,我收到以下错误:
SEVERE: Error parsing input: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:817)
at com.google.gson.Gson.fromJson(Gson.java:770)
at com.google.gson.Gson.fromJson(Gson.java:719)
at org.jclouds.json.internal.GsonWrapper.fromJson(GsonWrapper.java:42)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:83)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:77)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:62)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:42)
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.$Proxy57.list(Unknown Source)
Caused by: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$IterableTypeAdapter.readAndBuild(NullFilteringTypeAdapterFactories.java:88)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$IterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:82)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$FluentIterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:239)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$FluentIterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:225)
at com.google.gson.Gson.fromJson(Gson.java:805)
下面的代码取自执行时将失败的示例containerApi.list()
。正如所评论的那样,方法createContainer
和uploadObjectFromString
将起作用。
我只能认为 Swift 以 JSON 格式返回的响应与 JClouds 的预期略有不同,但我不知道这是否属实或如何解决。另外,我在我的 OpenStack 仪表板中看到,虽然对象存储是http://xx.xx.xx.107:80/swift/v1 身份是http://xx.xx.xx.101:5000/v2.0 不是确定为什么其中一个是 v1 而另一个是 v2.0,以及在使用 Java 库时是否应该考虑到这一点。我认为我对使用哪些 Java 库没有限制,所以我想知道是否最好使用另一个而不是 JClouds,尽管这个看起来最合适。
import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
import org.jclouds.ContextBuilder;
import org.jclouds.openstack.swift.v1.SwiftApi;
import org.jclouds.openstack.swift.v1.domain.Container;
import org.jclouds.openstack.swift.v1.features.ContainerApi;
import com.google.common.io.Closeables;
public class JCloudsSwift implements Closeable {
private SwiftApi swiftApi;
public static void main(String[] args) {
JCloudsSwift jcloudsSwift = new JCloudsSwift();
try {
jcloudsSwift.listContainers();
jcloudsSwift.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
jcloudsSwift.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public JCloudsSwift() {
String provider = "openstack-swift";
String identity = "tenantName:userName";
String credential = "password";
String endPoint = "http://xx.xx.xx.101:5000/v2.0/";
swiftApi = ContextBuilder.newBuilder(provider)
.endpoint(endPoint)
.credentials(identity, credential)
.buildApi(SwiftApi.class);
}
private void listContainers() {
String region = "us-east-1a";
ContainerApi containerApi = swiftApi.getContainerApi(region);
Set<Container> containers = containerApi.list().toSet();
for (Container container : containers) {
System.out.println(" " + container);
}
}
public void close() throws IOException {
Closeables.close(swiftApi, true);
}
}
按照 zachsh 的建议,我启用了有线日志(见下文)。我隐藏了一些细节(IP ...)。希望我在那里没有妥协任何东西。
在我看来,起初,有一个对身份 v2 端点的调用,它获得了一个有效的 JSON 响应,提供了不同的端点,比如带有 swift v1 的那个。请求和响应中的内容类型都是application/json
. 然后,调用这个 swift 端点,但它只会以纯文本形式返回响应。也就是说,请求中的请求内容类型是,application/json
响应中的内容类型是text/plain
。“Dev”和“dev2”是我在对象存储中的两个容器的名称。
我想知道我的 Java 代码中是否有任何问题(我主要复制了示例,但我可能配置错误),或者我使用的库的某些问题,甚至是我的 Openstack 安装中的某些问题(我无法控制它,但我可以询问具体情况)。
请参阅下面的日志。
jclouds-wire.log
2016-02-17 09:31:04,562 DEBUG [jclouds.wire] [main] >> "Sensitive data in payload, use PROPERTY_LOGGER_WIRE_LOG_SENSITIVE_INFO override to enable logging this data."
2016-02-17 09:31:04,562 DEBUG [jclouds.headers] [main] >> POST http://xx.xx.xx.101:5000/v2.0/tokens HTTP/1.1
2016-02-17 09:31:04,562 DEBUG [jclouds.headers] [main] >> Accept: application/json
2016-02-17 09:31:04,562 DEBUG [jclouds.headers] [main] >> Content-Type: application/json
2016-02-17 09:31:04,562 DEBUG [jclouds.headers] [main] >> Content-Length: 103
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << HTTP/1.1 200 OK
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << X-Distribution: Ubuntu
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << Connection: keep-alive
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << Vary: X-Auth-Token
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << Date: Wed, 17 Feb 2016 09:31:27 GMT
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << Content-Type: application/json
2016-02-17 09:31:04,984 DEBUG [jclouds.headers] [main] << Content-Length: 4421
2016-02-17 09:31:04,999 DEBUG [jclouds.wire] [main] << "{"access": {"token": {"issued_at": "2016-02-17T09:31:27.785179", "expires": "2016-02-17T10:31:27Z", "id": "fb6b46bd674046ef87c3270f1fd9dafd", "tenant": {"description": "", "enabled": true, "id": "c2c919f7f89a4d8f913e4ce19a0e6ace", "name": "my_tenant_name"}, "audit_ids": ["nIjY-heVSV-cbTfVIpEY-Q"]}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://xx.xx.xx.102:8774/v2/c2c919f7f89a4d8f913e4ce19a0e6ace", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.102:8774/v2/c2c919f7f89a4d8f913e4ce19a0e6ace", "id": "70c3eef4d3e242348c0e580f94414823", "publicURL": "http://xx.xx.xx.102:8774/v2/c2c919f7f89a4d8f913e4ce19a0e6ace"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.105:9696", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.105:9696", "id": "2a9328b17cc54538a38fb6ae13e5b3ac", "publicURL": "http://xx.xx.xx.105:9696"}], "endpoints_links": [], "type": "network", "name": "quantum"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.104:8776/v2/c2c919f7f89a4d8f913e4ce19a0e6ace", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.104:8776/v2/c2c919f7f89a4d8f913e4ce19a0e6ace", "id": "2eda4420ba0a49f9891ed9bd803b569c", "publicURL": "http://xx.xx.xx.104:8776/v2/c2c919f7f89a4d8f913e4ce19a0e6ace"}], "endpoints_links": [], "type": "volumev2", "name": "cinderv2"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.102:3333", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.102:3333", "id": "538aab805479410da21399abd012ceff", "publicURL": "http://xx.xx.xx.102:3333"}], "endpoints_links": [], "type": "s3", "name": "s3"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.103:9292", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.103:9292", "id": "80559923bf354541bdd9f56a45bedd7a", "publicURL": "http://xx.xx.xx.103:9292"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.108:8777", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.108:8777", "id": "a777efdc724643ecb6e705ff2eb47004", "publicURL": "http://xx.xx.xx.108:8777"}], "endpoints_links": [], "type": "metering", "name": "ceilometer"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.220:8000/v1", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.220:8000/v1", "id": "253135e684c24e00bb7bea6feb7cf595", "publicURL": "http://xx.xx.xx.220:8000/v1"}], "endpoints_links": [], "type": "cloudformation", "name": "heat-cfn"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.104:8776/v1/c2c919f7f89a4d8f913e4ce19a0e6ace", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.104:8776/v1/c2c919f7f89a4d8f913e4ce19a0e6ace", "id": "38264ccfbb574fcd81ba345c9eb7d37b", "publicURL": "http://xx.xx.xx.104:8776/v1/c2c919f7f89a4d8f913e4ce19a0e6ace"}], "endpoints_links": [], "type": "volume", "name": "cinder"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.102:8773/services/Cloud", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.102:8773/services/Cloud", "id": "4da43f1139c94d8aaf82564e16156ca5", "publicURL": "http://xx.xx.xx.102:8773/services/Cloud"}], "endpoints_links": [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.220:8004/v1/c2c919f7f89a4d8f913e4ce19a0e6ace", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.220:8004/v1/c2c919f7f89a4d8f913e4ce19a0e6ace", "id": "1180dc51deea4523a088871a352d9f72", "publicURL": "http://xx.xx.xx.220:8004/v1/c2c919f7f89a4d8f913e4ce19a0e6ace"}], "endpoints_links": [], "type": "orchestration", "name": "heat"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.107:80/swift", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.107:80/swift/v1", "id": "0457cd31064243c9b548dfe2eb0c1bc8", "publicURL": "http://xx.xx.xx.107:80/swift/v1"}], "endpoints_links": [], "type": "object-store", "name": "swift"}, {"endpoints": [{"adminURL": "http://xx.xx.xx.101:35357/v2.0", "region": "us-east-1a", "internalURL": "http://xx.xx.xx.101:5000/v2.0", "id": "40d04c8899cc4ea083dc5ecac808a825", "publicURL": "http://xx.xx.xx.101:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "my_user_name", "roles_links": [], "id": "e9312e8a3579404d91ba53894dcf72ea", "roles": [{"name": "Member"}, {"name": "_member_"}], "name": "my_user_name"}, "metadata": {"is_admin": 0, "roles": ["647df06e706f4d1f8a8810246e19bd26", "9fe2ff9ee4384b1894a90878d3e92bab"]}}}"
2016-02-17 09:31:05,062 DEBUG [jclouds.headers] [main] >> GET http://xx.xx.xx.107:80/swift/v1 HTTP/1.1
2016-02-17 09:31:05,062 DEBUG [jclouds.headers] [main] >> Accept: application/json
2016-02-17 09:31:05,062 DEBUG [jclouds.headers] [main] >> X-Auth-Token: fb6b46bd674046ef87c3270f1fd9dafd
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << HTTP/1.1 200 OK
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << Content-type: text/plain; charset=utf-8
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << Connection: Keep-Alive
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << Date: Wed, 17 Feb 2016 09:31:28 GMT
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << Content-Type: text/plain; charset=utf-8
2016-02-17 09:31:05,405 DEBUG [jclouds.headers] [main] << Content-Length: 9
2016-02-17 09:31:05,405 DEBUG [jclouds.wire] [main] << "dev[\n]"
2016-02-17 09:31:05,405 DEBUG [jclouds.wire] [main] << "dev2[\n]"
jclouds.log
2016-02-17 09:31:04,562 DEBUG [org.jclouds.rest.internal.InvokeHttpMethod] [main] >> invoking AuthenticationApi.authenticateWithTenantNameAndCredentials
2016-02-17 09:31:04,562 DEBUG [org.jclouds.http.internal.JavaUrlHttpCommandExecutorService] [main] Sending request -1271935931: POST http://xx.xx.xx.101:5000/v2.0/tokens HTTP/1.1
2016-02-17 09:31:04,984 DEBUG [org.jclouds.http.internal.JavaUrlHttpCommandExecutorService] [main] Receiving response -1271935931: HTTP/1.1 200 OK
2016-02-17 09:31:05,046 DEBUG [org.jclouds.openstack.keystone.v2_0.suppliers.RegionIdToURIFromAccessForTypeAndVersion] [main] endpoints for apiType object-store and version 1: {us-east-1a=[Endpoint{id=0457cd31064243c9b548dfe2eb0c1bc8, region=us-east-1a, publicURL=http://xx.xx.xx.107:80/swift/v1, internalURL=http://xx.xx.xx.107:80/swift/v1, adminURL=http://xx.xx.xx.107:80/swift}]}
2016-02-17 09:31:05,062 DEBUG [org.jclouds.rest.internal.InvokeHttpMethod] [main] >> invoking container:list
2016-02-17 09:31:05,062 DEBUG [org.jclouds.http.internal.JavaUrlHttpCommandExecutorService] [main] Sending request -629919459: GET http://xx.xx.xx.107:80/swift/v1 HTTP/1.1
2016-02-17 09:31:05,405 DEBUG [org.jclouds.http.internal.JavaUrlHttpCommandExecutorService] [main] Receiving response -629919459: HTTP/1.1 200 OK
2016-02-17 09:31:05,420 ERROR [org.jclouds.http.functions.ParseJson] [main] Error parsing input: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:817)
at com.google.gson.Gson.fromJson(Gson.java:770)
at com.google.gson.Gson.fromJson(Gson.java:719)
at org.jclouds.json.internal.GsonWrapper.fromJson(GsonWrapper.java:42)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:83)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:77)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:62)
at org.jclouds.http.functions.ParseJson.apply(ParseJson.java:42)
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.$Proxy57.list(Unknown Source)
at com.ceph.JCloudsSwift.listContainers(JCloudsSwift.java:58)
at com.ceph.JCloudsSwift.main(JCloudsSwift.java:22)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$IterableTypeAdapter.readAndBuild(NullFilteringTypeAdapterFactories.java:88)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$IterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:82)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$FluentIterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:239)
at org.jclouds.json.internal.NullFilteringTypeAdapterFactories$FluentIterableTypeAdapter.read(NullFilteringTypeAdapterFactories.java:225)
at com.google.gson.Gson.fromJson(Gson.java:805)
... 15 common frames omitted