加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Spring Cloud AWS代码未找到S3文件

发布时间:2020-12-15 02:13:53 所属栏目:Java 来源:网络整理
导读:我不明白为什么 Spring AWS Cloud代码没有找到我的S3文件.我的spring bean xml配置中有’aws-context:context-resource-loader’.我本以为’s3:’资源的使用就像春天一样无缝’classpath:’资源很容易使用. 我知道AWS权限和凭据配置正确,因为如果我直接使
我不明白为什么 Spring AWS Cloud代码没有找到我的S3文件.我的spring bean xml配置中有’aws-context:context-resource-loader’.我本以为’s3:’资源的使用就像春天一样无缝’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构造函数中有一个显示此ResourceLoader的System.out.println:

org.springframework.context.annotation.AnnotationConfigApplicationContext

我得到的例外是:

Exception in thread “main” java.io.FileNotFoundException: class path resource [s3://MyBucket/sample/resource.txt] cannot be opened because it does not exist

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

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

解决方法

一个问题是ResoureLoader对S3的支持不是读取region属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读