我正在使用 Spring Boot 1.5.1.RELEASE 与 Spring Cloud AWS 1.1.3.RELEASE 将文件上传到AWS S3存储桶。
我想使用TransferManager将文件上传到S3。 但不幸的是,我在上传过程中收到以下错误消息,并且文件未上传到S3:
2017-02-26 12:36:27.004 ERROR 32696 --- [ask-scheduler-8] o.s.integration.handler.LoggingHandler : com.amazonaws.AmazonClientException: Unable to complete transfer: Connection pool shut down
at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.unwrapExecutionException(AbstractTransfer.java:277)
at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.rethrowExecutionException(AbstractTransfer.java:261)
at com.amazonaws.services.s3.transfer.internal.UploadImpl.waitForUploadResult(UploadImpl.java:66)
at com.my.package.aws.S3Configuration.withTransferManager(S3Configuration.java:50)
at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这些是我的Java配置文件:
@EnableContextResourceLoader
@EnableContextCredentials(accessKey="XXXX", secretKey="YYYY")
@EnableAsync
@ComponentScan("com.my.package")
@EnableJpaRepositories(basePackages = "com.my.package.repository")
@EnableScheduling
@SpringBootApplication
public class S3UploadApplication {
public static void main(final String[] args) {
SpringApplication.run(S3UploadApplication.class, args);
}
}
S3的配置bean:
@Configuration
public class S3Configuration {
@Autowired
private AmazonS3Client amazonS3client;
public void withTransferManager() {
TransferManager transferManager = new TransferManager(this.amazonS3client);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
Upload uploaded=transferManager.upload("myBucket","test.txt",new FileInputStream(new File("TestFile.txt")),objectMetadata);
uploaded.waitForCompletion();
transferManager.shutdownNow();
}
}
我也尝试了这个solution ,即明确地创建了两个Beans BasicAWSCredentials和AmazonS3Client并进行了配置,但仍然显示相同的错误。
答案 0 :(得分:0)
我认为您在某些地方关闭了S3客户端。或者与TransferManager相关的代码被调用一次。在aws sdk https://github.com/aws/aws-sdk-java/issues/1282
中看到此问题后因此,解决方案是每次需要使用它时创建一个新的S3 Client和TransferManager实例,或者只是不关闭它们。
从问题来看,最佳做法似乎是不要关闭S3客户端/传输管理器,因为它提供了更好的性能。
答案 1 :(得分:0)
从上述代码中,单个AmazonS3客户端被重用 在其中为每个s3操作transfermanager对象创建一个。 Shutdownnow()方法将关闭传输管理器以及AmazonS3客户端。
因此,AmazonS3客户端无法再次重用。
aws的关机方法文档:
代替shutdownnow(),请尝试使用shutdownnow(false),它将仅关闭传输管理器,因此仍可以使用AmazonS3客户端。