The test dummy server for S3 is Fake S3. I heard that it seems to be Ruby, and since I am a Java shop, I searched for Java, and found S3 ninja.
So let's use it. Let's do so. Fake S3 is coming again.
Normally it seems to download and use it, but since it is troublesome, this time I will install it with maven and force it to start with Junit.
pom.xml
pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.91</version>
</dependency>
<dependency>
<groupId>com.scireum</groupId>
<artifactId>s3ninja</artifactId>
<version>2.7</version>
</dependency>
Test.java
static {
//Dig the required folder
try {
Files.createDirectories(Paths.get("data/s3"));
} catch (IOException e) {
}
//Start s3ninja
Thread thread = new Thread(() -> {
try {
Method main = Class.forName("IPL").getMethod("main", new Class[] { String[].class });
main.invoke(null, (Object) new String[0]);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
thread.start();
try {
Thread.sleep(1000);//Just in case
} catch (InterruptedException e) {
}
}
@Test
public void test() throws IOException {
AmazonS3 s3 = AmazonS3Client.builder()
.withEndpointConfiguration(new EndpointConfiguration("http://localhost:9444/s3", null))
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")))
.withPathStyleAccessEnabled(true)
.withClientConfiguration(new ClientConfiguration().withSignerOverride("S3SignerType"))
.build();
//upload
s3.putObject(new PutObjectRequest("bucket", "s3test.txt", new File("test.txt")));
//download
try (S3Object s3Object = s3.getObject(new GetObjectRequest("bucket", "s3test.txt"));
InputStream input = s3Object.getObjectContent()) {
assertThat(IOUtils.toByteArray(input), is(Files.readAllBytes(Paths.get("test.txt"))));
}
}
./data/s3
to use it, so dig it.reference: I put in a local S3 ninja
Recommended Posts