用于集成测试的AWS S3 Java嵌入式模拟

用于集成测试的AWS S3 Java嵌入式模拟,java,amazon-web-services,amazon-s3,integration-testing,s3proxy,Java,Amazon Web Services,Amazon S3,Integration Testing,S3proxy,在互联网上搜索一个好的嵌入式Java AWS S3模拟解决方案后,它似乎是最流行的解决方案 然而,似乎并没有一个简单的方法以编程方式启动这些。在放弃S3Ninja之后,我试着用S3Proxy来做,但它不太管用 Maven依赖关系 <dependency> <groupId>org.gaul</groupId> <artifactId>s3proxy</artifactId> <version>${s3

在互联网上搜索一个好的嵌入式Java AWS S3模拟解决方案后,它似乎是最流行的解决方案

然而,似乎并没有一个简单的方法以编程方式启动这些。在放弃S3Ninja之后,我试着用S3Proxy来做,但它不太管用

Maven依赖关系

<dependency>
    <groupId>org.gaul</groupId>
    <artifactId>s3proxy</artifactId>
    <version>${s3proxy.version}</version>
    <scope>test</scope>
</dependency>

高卢
S3代理
${s3proxy.version}
测试
代码

String endpoint = "http://127.0.0.1:8085";
URI uri = URI.create(endpoint);
Properties properties = new Properties();
properties.setProperty("s3proxy.authorization", "none");
properties.setProperty("s3proxy.endpoint", endpoint);
properties.setProperty("jclouds.provider", "filesystem");
properties.setProperty("jclouds.filesystem.basedir", "/tmp/s3proxy");

ContextBuilder builder = ContextBuilder
        .newBuilder("filesystem")
        .credentials("x", "x")
        .modules(ImmutableList.<Module>of(new SLF4JLoggingModule()))
        .overrides(properties);
BlobStoreContext context = builder.build(BlobStoreContext.class);
BlobStore blobStore = context.getBlobStore();

S3Proxy s3Proxy = S3Proxy.builder().awsAuthentication("x", "x").endpoint(uri).keyStore("", "").blobStore(blobStore).build();
s3Proxy.start();

BasicAWSCredentials awsCredentials = new BasicAWSCredentials("x", "x");

AmazonS3Client client = new AmazonS3Client(awsCredentials, new ClientConfiguration());
client.setEndpoint(endpoint);

// Should Throw AWS Client Exception as Bucket / Key does not exist!
GetObjectRequest objectRequest = new GetObjectRequest("bucket", "key");
S3Object object = client.getObject(objectRequest);

s3Proxy.stop();
String端点=”http://127.0.0.1:8085";
URI=URI.create(端点);
属性=新属性();
properties.setProperty(“s3proxy.authorization”、“none”);
setProperty(“s3proxy.endpoint”,endpoint);
setProperty(“jclouds.provider”、“filesystem”);
properties.setProperty(“jclouds.filesystem.basedir”,“/tmp/s3proxy”);
ContextBuilder=ContextBuilder
.newBuilder(“文件系统”)
.全权证书(“x”、“x”)
.modules(ImmutableList.of(新的SLF4JLoggingModule()))
.覆盖(属性);
BlobStoreContext=builder.build(BlobStoreContext.class);
BlobStore BlobStore=context.getBlobStore();
S3Proxy S3Proxy=S3Proxy.builder().awsAuthentication(“x”,“x”).endpoint(uri).keyStore(“,”).blobStore(blobStore).build();
s3Proxy.start();
BasicAWSCredentials awsCredentials=新的BasicAWSCredentials(“x”、“x”);
AmazonS3Client client=新的AmazonS3Client(awsCredentials,new ClientConfiguration());
setEndpoint(端点);
//由于Bucket/Key不存在,应引发AWS客户端异常!
GetObjectRequest objectRequest=新的GetObjectRequest(“bucket”、“key”);
S3Object对象=client.getObject(objectRequest);
s3Proxy.stop();
例外情况

java.lang.NoSuchMethodError: com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(Lcom/google/gson/internal/ConstructorConstructor;Lcom/google/gson/FieldNamingStrategy;Lcom/google/gson/internal/Excluder;)V

at org.jclouds.json.internal.DeserializationConstructorAndReflectiveTypeAdapterFactory.<init>(DeserializationConstructorAndReflectiveTypeAdapterFactory.java:116)
at org.jclouds.json.config.GsonModule.provideGson(GsonModule.java:129)

...

at org.jclouds.providers.config.BindProviderMetadataContextAndCredentials.backend(BindProviderMetadataContextAndCredentials.java:84)

...

at org.jclouds.ContextBuilder.build(ContextBuilder.java:581)
java.lang.NoSuchMethodError:com.google.gson.internal.bind.ReflectTypeAdapterFactory.(Lcom/google/gson/internal/ConstructorConstructor;Lcom/google/gson/FieldNamingStrategy;Lcom/google/gson/internal/Excluder;)V
位于org.jclouds.json.internal.DeserializationConstructorAndReflectionTypeAdapterFactory。(DeserializationConstructorAndReflectionTypeAdapterFactory.java:116)
位于org.jclouds.json.config.GsonModule.provideGson(GsonModule.java:129)
...
位于org.jclouds.providers.config.BindProviderMetadataContextAndCredentials.backend(BindProviderMetadataContextAndCredentials.java:84)
...
位于org.jclouds.ContextBuilder.build(ContextBuilder.java:581)

非常感谢您的帮助。我敢肯定,对于许多与AWS S3交互的Java集成测试来说,这是一个很大的需求。

只是想说明一下原因,因为您的项目使用的是一个相互冲突的gson版本。S3Proxy的dep需要gson 2.5。

如果它是一个集成测试,你不想让它与真正的S3对话吗?不想。我们的集成测试应该能够在没有任何其他组件的情况下本地运行。我们使用ActiveMQ模拟SQS和embedded Redis for ElasticCache。@ptimson为了支持Rob的评论,您的集成测试环境应该与生产环境相同。它听起来更像是开发人员环境,而不是集成环境。有用于测试的模拟SQS服务。查看@jbird抱歉,我是说我们使用的是ElasticMQ而不是ActiveMQ。还有ElasticCache。我们正在寻找一个S3等效。这不是一个环境,而是一个将使用JUnit运行的测试。这个问题的目的是确定如何设置嵌入式S3,如ElasticMQ和embedded Redis。我认为我们中的一些人对“集成测试”(与单元测试)结合模拟服务的上下文感到困惑。模拟服务对于单元测试来说可能是个好主意,但我认为模拟对于集成测试来说是错误的。话虽如此,我们发现即使对于单元测试,AWS服务也是可以接受的依赖项(尽管这种单元测试在实践中很少)。很抱歉,因为我们的方法与你的不同,我无法回答你的问题。但这可能会提供一个线索,说明为什么你没有得到有用的答案。