Java – spring cloud AWS code did not find S3 file

I don't understand why the spring AWS cloud code didn't find my S3 file My spring bean XML configuration has' AWS context: context resource loader ' I thought the use of resources was as seamless as spring. Classpath: resources are easy to use

I know that AWS permissions and credentials are configured correctly, because if I use Amazon S3 client directly, I can retrieve the file in question

Starting from the spring side, AWS credentials should be found automatically

This is a valid Amazon S3 client 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);    
}

The following is the spring cloud AWS equivalent for which the same S3 file was not found:

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);
    }
}

I have a system. In the s3resource constructor that displays this resourceloader out. println:

The exceptions I get are:

S3Resource:

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();
    }
}

Finally, my spring XML

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <aws-context:context-resource-loader/>
    <aws-context:context-region region="us-east-1"/>
</beans>

The GitHub link is basically the same as the following code: https://github.com/BDF/SpringCloudSample

Solution

One problem is that resourceloader supports S3 instead of reading the region attribute

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>