【发布时间】:2015-10-01 13:54:36
【问题描述】:
我正在尝试使用 Spring Cloud 项目将文件上传到 Amazon S3 存储桶。
根据文档:
“一个com.amazonaws.services.s3.transfer.TransferManager可以很容易 在应用程序代码中创建并注入预配置的 com.amazonaws.services.s3.AmazonS3 客户端已使用 Spring Cloud AWS 资源加载器配置。”
此示例代码不起作用:
public class SimpleResourceLoadingBean {
@Autowired
private AmazonS3 amazonS3;
public void withTransferManager() {
TransferManager transferManager = new TransferManager(this.amazonS3);
transferManager.upload("myBucket","filename",new File("someFile"));
}
}
amazonS3 变量始终为空。
我的 spring 上下文配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd">
<aws-context:context-credentials>
<aws-context:simple-credentials access-key="KEY" secret-key="SECRET" />
</aws-context:context-credentials>
<aws-context:context-region region="eu-central-1"/>
<aws-context:context-resource-loader/>
<bean id="awsTransferManager" class="mytest.cloud.aws.AwsTransferManager" />
我正在使用这个类进行测试:
public class AwsTransferManager {
@Autowired
private AmazonS3 amazonS3;
public void upload() throws AmazonServiceException, AmazonClientException, InterruptedException {
TransferManager tx = new TransferManager(amazonS3);
System.out.println(amazonS3); //always null
}
测试:
@Test
public void awsTest() throws Exception {
ApplicationContext cxt = new ClassPathXmlApplicationContext("META-INF/spring/spring-context.xml");
AwsTransferManager manager = (AwsTransferManager)getContext().getBean("awsTransferManager");
manager.upload();
}
【问题讨论】:
标签: spring amazon-web-services amazon-s3 spring-cloud