【问题标题】:Spring Cloud AWS code not finding S3 FileSpring Cloud AWS 代码找不到 S3 文件
【发布时间】:2016-03-25 15:15:51
【问题描述】:

我不明白为什么 Spring AWS Cloud 代码找不到我的 S3 文件。我的 spring bean xml 配置中有“aws-context:context-resource-loader”。我原以为 's3:' 资源使用会像 spring 一样无缝,'classpath:' 资源很容易使用。

我知道 AWS 权限和凭证配置正确,因为如果我直接使用 AmazonS3Client,我可以检索相关文件。

从 Spring side 开始,应该会自动找到 AWS 凭证。

这是工作的 Amazon S3 客户端代码:

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

这是未找到相同 S3 文件的 Spring Cloud AWS 等效项:

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

我在 S3Resource 构造函数中有一个 System.out.println,它显示了这个 ResourceLoader:

org.springframework.context.annotation.AnnotationConfigApplicationContext

我得到的例外是:

线程“main”java.io.FileNotFoundException 中的异常:类路径资源 [s3://MyBucket/sample/resource.txt] 无法打开,因为它不存在

S3 资源:

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

最后是我的 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>

github 链接的代码基本相同:https://github.com/BDF/SpringCloudSample

【问题讨论】:

    标签: java spring amazon-s3


    【解决方案1】:

    一个问题是对 S3 的 ResoureLoader 支持没有读取区域属性。

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 2018-12-20
      • 2023-04-07
      • 2020-10-12
      • 2019-05-24
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多