3

I understand that spring-cloud-aws offers a SimpleStorageResourceLoader to allow loading of resources from S3 with path patterns like s3://<bucket>/<key>. The problem I am having is how to make sure that ResourceLoader gets used when resolving static resources within the mvc component. Here is my configuration for the resource mapping:

@Configuration
public class TestWebConfigurer extends WebMvcConfigurerAdapter {
    private String s3Location;

    // ${test.s3.location} comes in as "s3://<bucket>/"   
    public TestWebConfigurer(@Value("${test.s3.location}") String s3Location) {
        this.s3Location = s3Location;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/mytest/**").addResourceLocations(s3Location);
    }
}

When I step through the code within the addResourceLocations() there is a call there to the resourceLoader.getResource(location). Unfortunately that ends up being just the DefaultResourceLocator and when the Resource is returned it ends up being a ServletContextResource with path of /s3://<bucket>/.

I have configured my aws credentials within my application.yml file as such:

cloud:
  aws:
    credentials:
      accessKey: <myaccess>
      secretKey: <mysecret>
    region:
      static: us-east-1

I am not doing anything special to autowire or startup any AWS specific beans as my understanding was that the spring-cloud-starter-aws would do that for me within the context of Spring Boot.

I was able to create an ApplicationRunner within the same project to test that my AWS connectivity was correct. Here is the test:

@Component
public class TestSimpleStorageLoader implements ApplicationRunner {
    private ResourceLoader loader;

    public TestSimpleStorageLoader(ResourceLoader loader) {
        this.loader = loader;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Resource resource = this.loader.getResource("s3://<bucket>/test.txt");

        IOUtils.copy(resource.getInputStream(), System.out);
    }
}

This test was able to output the contents of the test.txt file on application startup.

What am I missing to get this to work for the resource handler?

Shawn Clark
  • 3,330
  • 2
  • 18
  • 30
  • Shawn, I found that for some reason on ResourceHandlers registration s3-protocol is undefined. Furthermore DefaultResourceLoader used instead of SimpleStorageResourceLoader. And it looks like problem can be resolved by using ProtocolResolver: https://jira.spring.io/browse/SPR-12857 – Konstantin Konyshev Aug 26 '16 at 11:31
  • Have you found a solution? Could you share some more code? – Battle_Slug Nov 16 '16 at 05:25

0 Answers0