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

标签 java amazon-web-services amazon-s3 integration-testing s3proxy

在互联网上搜索嵌入式 Java AWS S3 模拟的良好解决方案后,似乎 S3NinjaS3Proxy似乎是最受欢迎的解决方案。

然而,似乎没有一种简单的方法来以编程方式启动它们。在放弃使用 S3Ninja 后,我尝试使用 S3Proxy 来实现,但效果不佳。

Maven 依赖项

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

代码

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();

异常

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)

非常感谢任何帮助。我确信这对于许多与 AWS S3 交互的 Java 集成测试来说是一个很大的要求。

最佳答案

只是评论原因是因为您的项目正在使用冲突版本的 gson。 S3Proxy 的 dep 需要 gson 2.5。

关于java - 用于集成测试的 AWS S3 Java 嵌入式模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170115/

相关文章:

java - 如何在 Android 上使用 Wobly 序列化器

amazon-web-services - Amazon ECS LogConfiguration 返回 "unsupported property logConfiguration"

amazon-web-services - 代码管道 : Insufficient permissions Unable to access the artifact with Amazon S3 object key

amazon-web-services - 如何使 ElastiCache 上的 Redis 事件触发 Lambda 函数?

python-3.x - 使用 livy 向 AWS EMR 提交具有虚拟环境的 pyspark 作业

amazon-web-services - 列出具有公共(public)读取权限的 S3 对象(在私有(private)存储桶中)

ruby - 使用 Ruby 文件设置内容类型创建的文件?

java - 未定义名为 'springSecurityFilterChain' 的 bean

java - 找到连接最大可能点数的方程式

java - 用在该字符串上调用的方法替换与模式匹配的每个匹配项