title: Spring Cloud AWS code not finding S3 File
body: <p>I do not understand why the Spring AWS Cloud code is not finding my S3 file.  I have the 'aws-context:context-resource-loader' in my spring bean xml configuration.  I would have thought the 's3:' resource usage would be seamless like spring the 'classpath:' resource is easy to use.</p>
  
  <p>I know that the AWS permissions and credentials are configured correctly because I can retrieve the file in question if I use the       AmazonS3Client directly.  </p>
  
  <p>From the Spring <a href="http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sdk_credentials_configuration" rel="nofollow">side</a> the AWS credentials should be found  automatically.  </p>
  
  <p>Here is the working Amazon S3 Client code:</p>
  
  <pre><code>public static void main(String [] args) throws IOException {
    AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
    System.out.println(s3Client.doesObjectExist("MyBucket", "sample/resource.txt"));
    S3Object object = s3Client.getObject(new GetObjectRequest("MyBucket", "sample/resource.txt"));
    String result = StreamUtils.copyToString(object.getObjectContent(), Charset.forName("UTF-8"));
    System.out.println(result);    
  }
  </code></pre>
  
  <p>Here is the Spring Cloud AWS equivalent that is not finding the same S3 file:</p>
  
  <pre><code>public class App {
      public static void main(String[] args) throws IOException {
          final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("app-context.xml");
          ctx.scan("bdf.sample.spring");
          S3Resource s3Resource = ctx.getBean("S3Resource", S3Resource.class);
          InputStream resource = s3Resource.getResource("s3://MyBucket/sample/resource.txt");
          String result = StreamUtils.copyToString(resource, Charset.forName("UTF-8"));
          System.out.println(result);
      }
  }
  </code></pre>
  
  <p>I have a System.out.println in the S3Resource constructor that shows this ResourceLoader:</p>
  
  <blockquote>
    <p>org.springframework.context.annotation.AnnotationConfigApplicationContext</p>
  </blockquote>
  
  <p>The Exception I get is:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.io.FileNotFoundException: class path resource [s3://MyBucket/sample/resource.txt] cannot be opened because it does not exist</p>
  </blockquote>
  
  <p>The S3Resource:</p>
  
  <pre><code>package bdf.sample.spring;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.core.io.Resource;
  import org.springframework.core.io.ResourceLoader;
  import org.springframework.stereotype.Service;
  import java.io.IOException;
  import java.io.InputStream;
  
  @Service("S3Resource")
  public class S3Resource  {
      private final ResourceLoader resourceLoader;
  
      @Autowired
      public S3Resource(ResourceLoader resourceLoader) {
          this.resourceLoader = resourceLoader;
          System.out.println(resourceLoader.getClass());
      }
      public InputStream getResource(String path) throws IOException {
          final Resource resource = resourceLoader.getResource(path);
          return resource.getInputStream();
      }
  }
  </code></pre>
  
  <p>And finally my Spring XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/cloud/aws/context
          http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd"&gt;
  
      &lt;aws-context:context-resource-loader/&gt;
      &lt;aws-context:context-region region="us-east-1"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>github link with esentially the same code:  <a href="https://github.com/BDF/SpringCloudSample" rel="nofollow">https://github.com/BDF/SpringCloudSample</a></p>