Le serveur factice de test pour S3 est Fake S3. J'ai entendu dire que cela semble être Ruby, et comme je suis une boutique Java, j'ai cherché Java et j'ai trouvé S3 ninja.

Alors utilisons-le. Faisons-le. Fake S3 revient.
Normalement, il semble être téléchargé et utilisé, mais comme c'est gênant, cette fois je vais l'installer avec maven et le forcer à démarrer avec 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 {
		//Creusez le dossier requis
		try {
			Files.createDirectories(Paths.get("data/s3"));
		} catch (IOException e) {
		}
		//Démarrer 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);//Au cas où
		} 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();
		//télécharger
		s3.putObject(new PutObjectRequest("bucket", "s3test.txt", new File("test.txt")));
		//Télécharger
		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 pour l'utiliser, alors creusez-le.référence: J'ai mis un ninja appelé local S3
Recommended Posts