Java 如何在集成测试中模拟 Amazon S3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6615988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:18:19  来源:igfitidea点击:

How to mock Amazon S3 in an integration test

javajunitamazon-s3integration-testing

提问by Michael Deardeuff

I'm trying to get an "walking skeleton" of my app up that will use S3 for persistence. I would like to use a fake S3 service so each developer's desktop can read/write at will.

我正在尝试获取我的应用程序的“行走骨架”,它将使用 S3 进行持久化。我想使用伪造的 S3 服务,以便每个开发人员的桌面都可以随意读/写。

I thought mocks3would be perfect, as I could get a jetty server up in my jUnit tests. The problem is that mocks3 doesn't allow any writes. Not even to set it up as far as I can tell.

我认为mocks3会很完美,因为我可以在我的 jUnit 测试中安装一个码头服务器。问题是 mocks3 不允许任何writes。据我所知,甚至没有设置它。

So how do others do this?

那么其他人是如何做到这一点的呢?

采纳答案by mikelikespie

Tornado, a python web framework, has an example app that is just what you're looking for.

Tornado 是一个 Python 网络框架,它有一个示例应用程序,正是您正在寻找的。

https://github.com/facebook/tornado/blob/master/demos/s3server/s3server.py

https://github.com/facebook/tornado/blob/master/demos/s3server/s3server.py

It can be used out of the box.

它可以开箱即用。

回答by Michael Deardeuff

One option is to scrap the jetty server and use Apache VFSand the S3 plugin. With that, you can use the memory or file-based storage implementations for the integration testing.

一种选择是废弃码头服务器并使用Apache VFSS3 插件。有了它,您可以使用内存或基于文件的存储实现进行集成测试。

回答by Jay P.

Another option is S3 ninja- emulates the S3 API for development and testing purposes.

另一种选择是S3 ninja- 模拟 S3 API 用于开发和测试目的。

回答by shutty

There is also an s3mocktool written exactly for this purpose. It mocks the essential parts of AWS S3 API on top of local filesystem:

还有一个专门为此目的编写的s3mock工具。它在本地文件系统之上模拟 AWS S3 API 的基本部分:

S3Mock api = S3Mock.create(8001, "/tmp/s3");
api.start();

AmazonS3Client client = new AmazonS3Client(new AnonymousAWSCredentials());
// use local API mock, not the AWS one
client.setEndpoint("http://127.0.0.1:8001");
client.createBucket("testbucket");
client.putObject("testbucket", "file/name", "contents");

It's also easily embeddable and configuration-less.

它也很容易嵌入且无需配置。

回答by Rackam36

You can use scality s3server, in can run on your machine either using node.js or via docker and it gives you a local S3 service instance. It's open source under a BSD license github.com/scality/s3

您可以使用 scality s3server,它可以使用 node.js 或通过 docker 在您的机器上运行,它为您提供本地 S3 服务实例。它是在 BSD 许可下开源的 github.com/scality/s3

回答by Jake Ham

Have a look at Adobe's S3Mock. This S3 Mock server can be started via a Docker container or JUnit 4/5 rules.

看看Adobe 的 S3Mock。这个 S3 Mock 服务器可以通过 Docker 容器或 JUnit 4/5 规则启动。

回答by Stimp

If you're ok with depending on a running docker container, and want something well-supported, you could consider using localstack

如果您可以依赖正在运行的 docker 容器,并且想要得到很好的支持,则可以考虑使用localstack

Before running your tests, start S3 like so:

在运行测试之前,像这样启动 S3:

docker run --name localstack -d -p 5000:5000 -e SERVICES=s3:5000 localstack/localstack

And then stop it when tests complete like so:

然后在测试完成时停止它,如下所示:

docker stop localstack

You'll need to configure your S3 client to point to localhost:5000 for tests. In Java, this can be done like so:

您需要将 S3 客户端配置为指向 localhost:5000 进行测试。在 Java 中,这可以像这样完成:

        AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
                 "http://localhost:5000", 
                 "us-west-2"))
            .build();