SpringBoot : ServletContext resource cannot resolve S3 URL

Ollaw :

I'm developing a Spring Boot application in which I'm integrating Amazon S3 service.
This class is my repository to access the S3 bucket :

public class S3FileRepository implements ImageRepository {

private String bucket;
private AmazonS3 s3Client;
private ResourceLoader resourceLoader;

public S3FileRepository(ResourceLoader resourceLoader, AmazonS3 s3Client, String bucket) {
    this.resourceLoader = resourceLoader;
    this.s3Client = s3Client;
    this.bucket = bucket;
}

private static String toS3Uri(String bucket, String imageName) {
    return String.format("s3://%s/%s", bucket, imageName);
}

@Override
public Resource getImage(String name) {
    return resourceLoader.getResource(S3FileRepository.toS3Uri(this.bucket, name).concat(this.IMAGE_EXTENSION));
}

using Spring Boot Autoconfiguration as suggested.
So in my pom.xml, among other things, I've

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-autoconfigure</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-context</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

Moreover I've an application.properties done like this:

cloud.aws.credentials.accessKey= (mykey)
cloud.aws.credentials.secretKey= (mysecret)
cloud.aws.credentials.instanceProfile=true
cloud.aws.region.static=eu-west-2
cloud.aws.stack.auto=false

The problem:

Everything works fine if i compile my project and then I simply run the JAR with java -jar target/myproject.jar, i correctly get the image that I ask for and everything is fine.

Instead if I run the project with the IDE default mvn spring-boot:run when I try to get an image (present in the bucket) an Exception occour saying following:

    ServletContext resource [/s3://mybucket/test.jpeg] cannot be resolved to URL because it does not exist
java.io.FileNotFoundException: ServletContext resource [/s3://mybucket/test.jpeg] cannot be resolved to URL because it does not exist

So what I think is that it throws an Exception because it's like it goes inside the jar to look for something that match s3://mybucket/test.jpeg but I can't get why, and why it happens only running the project with mvn spring-boot:run and not running the jar.

Mikael Gueck :

You're likely hitting the spring-cloud-aws issue #384 whereupon the spring-boot-devtools dependency, which is activated when you start the application from the IDE, activates a different code path in resource loading.

You can test whether you're hitting this issue by removing the spring-boot-devtools dependency from your pom.xml file, reloading the project in your IDE, and running the same test.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144824&siteId=1