从 S3 下载文件 >3Gb 失败并显示 "SocketTimeoutException: Read timed out"

Downloading files >3Gb from S3 fails with "SocketTimeoutException: Read timed out"

AWS Java SDK 1.9.3

在 Java 服务器应用程序中从 AWS S3 下载许多大文件 (~3Gb) 时,我不时收到 SocketTimeoutException,如下所示:

Caused by: com.amazonaws.AmazonClientException: Unable to store object contents to disk: Read timed out
 at com.amazonaws.services.s3.internal.ServiceUtils.downloadObjectToFile(ServiceUtils.java:270)
 at com.amazonaws.services.s3.internal.ServiceUtils.retryableDownloadS3ObjectToFile(ServiceUtils.java:344)
 at com.amazonaws.services.s3.transfer.TransferManager.call(TransferManager.java:737)
 ... 4 more
Caused by: java.net.SocketTimeoutException: Read timed out
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(SocketInputStream.java:152)
 at java.net.SocketInputStream.read(SocketInputStream.java:122)
 at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
 at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:554)
 at sun.security.ssl.InputRecord.read(InputRecord.java:509)
 at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
 at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
 at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
 at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:198)
 at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178)
 at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
 at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
 at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:151)
 at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
 at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
 at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
 at com.amazonaws.event.ProgressInputStream.read(ProgressInputStream.java:151)
 at java.security.DigestInputStream.read(DigestInputStream.java:161)
 at com.amazonaws.services.s3.internal.DigestValidationInputStream.read(DigestValidationInputStream.java:59)
 at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:73)
 at java.io.FilterInputStream.read(FilterInputStream.java:107)
 at com.amazonaws.services.s3.internal.ServiceUtils.downloadObjectToFile(ServiceUtils.java:265)
 ... 6 more

我使用 4 个线程从 S3 下载文件,所有线程都使用相同的 TransferManager 实例。如前所述,我使用的是 AWS SDK 1.9.3,只是偶尔会遇到此类异常。代码如下:

...
Download d = transferManager.download(currentBucket, remoteFileName, new File(tmpName));
d.waitForCompletion();
...

是不是SDK的问题?也许是 v1.9.3?

除了为ClientConfiguration增加socketTimeout,还有什么解决办法吗?

以下是我在 AWS SDK 1.9.3 中可以执行的操作:

ClientConfiguration config = new ClientConfiguration(); 
config.setConnectionTimeout(connectionTimeout);
config.setSocketTimeout(readTimeout); 
AmazonS3 s3 = new AmazonS3Client(credentials, config);