[Java] Test S3 upload / download using "S3 ninja"

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.

S3 ninja

ninja.jpg

So let's use it. Let's do so. Fake S3 is coming again.

Write a test code

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>

JUnit code

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"))));
		}

	}

Description


reference: I put in a local S3 ninja

Recommended Posts

[Java] Test S3 upload / download using "S3 ninja"
Upload and download notes in java on S3
Image upload using CarrierWave ~ Rspec test ~
[Note] Download from S3, upload to S3
Upload a file using Java HttpURLConnection
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
Primality test Java
Try using JobScheduler's REST-API --Java RestClient Test class-
Story of test automation using Appium [Android / java]
Sorting using java comparator
Comfortable download with JAVA
Scraping practice using Java ②
Java Unit Test Library-Artery-Sample
Scraping practice using Java ①
Download Java with Ansible