2

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.

ollaw
  • 2,086
  • 1
  • 20
  • 33
  • You'll need to specify both which IDE and the full stack trace of the error. (Note that unless you're needing to process the image locally it's a lot easier on everyone for you to just return an HTTP S3 URI to the client; jets3t makes generating signed URIs simpler.) – chrylis -cautiouslyoptimistic- Mar 20 '19 at 02:05

2 Answers2

3

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.

Mikael Gueck
  • 5,511
  • 1
  • 27
  • 25
0

There is a difference between app's launching with "java -jar" and "mvn spring-boot:run". From Spring Boot docs: "The Spring Boot Maven plugin includes a run goal that can be used to quickly compile and run your application. Applications run in an exploded form, as they do in your IDE". It can be a reason of problem.

JAlexey
  • 114
  • 4