I use Spring Cloud's ResourceLoader to access S3, e.g.:
public class S3DownUpLoader {
private final ResourceLoader resourceLoader;
@Autowired
public S3DownUpLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public String storeOnS3(String filename, byte[] data) throws IOException {
String location = "s3://" + bucket + "/" + filename;
WritableResource writeableResource = (WritableResource) this.resourceLoader.getResource(location);
FileCopyUtils.copy( data, writeableResource.getOutputStream());
return filename;
}
It works okey and I need help to test the code with Localstack/Testcontainers. I've tried following test, but it does not work - my production profile gets picked up(s3 client with localstack config is not injected):
@RunWith(SpringRunner.class)
@SpringBootTest
public class S3DownUpLoaderTest {
@ClassRule
public static LocalStackContainer localstack = new LocalStackContainer().withServices(S3);
@Autowired
S3DownUpLoader s3DownUpLoader;
@Test
public void testA() {
s3DownUpLoader.storeOnS3(...);
}
@TestConfiguration
@EnableContextResourceLoader
public static class S3Configuration {
@Primary
@Bean(destroyMethod = "shutdown")
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(localstack.getEndpointConfiguration(S3))
.withCredentials(localstack.getDefaultCredentialsProvider())
.build();
}
}
}