带有传输管理器的Spring云AWS:无法完成传输:连接池已关闭

b09cbbtk  于 2023-01-09  发布在  Spring
关注(0)|答案(2)|浏览(311)

我正在使用Sping Boot 1.5.1.RELEASESpring 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,即显式创建两个bean BasicAWSCredentials和amazons3client并相应地配置它,但仍然显示相同的错误。

kgsdhlau

kgsdhlau1#

从上面的代码中,可以重用一个AmazonS3客户端,其中为每个s3操作创建了transfermanager对象。Shutdownnow()方法关闭transfermanager和AmazonS3客户端。
因此,AmazonS3客户端不能再次重用。
aws关机方法文档:
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html
尝试使用shutdownnow(false)而不是shutdownnow(),它只会关闭transfermanager,因此AmazonS3客户端仍然可以使用。

ulmd4ohb

ulmd4ohb2#

我想你是在某个地方关闭了S3客户端。或者TransferManager相关代码被调用了不止一次。在aws sdk https://github.com/aws/aws-sdk-java/issues/1282中看到这个问题后
因此,解决方案是在每次需要使用S3客户端和TransferManager时创建一个新示例,或者只是不关闭它们。
从这个问题来看,最佳做法似乎是不要关闭S3客户端/传输管理器,因为这样可以提供更好的性能。

相关问题