[英]Java.net.SocketException: Connection reset when reading file from S3
I'm trying to read some files from S3 and do some processing on each file.我正在尝试从 S3 读取一些文件并对每个文件进行一些处理。 I can get through some files, but I keep getting Java.net.SocketException: Connection reset at the same line in a particular file during processing.我可以通过一些文件,但在处理过程中我不断收到Java.net.SocketException: Connection reset在特定文件中的同一行。 The file in question should be ok though because I can process it locally using the same class and method (conversionUtils.convert()).有问题的文件应该没问题,因为我可以使用相同的类和方法(conversionUtils.convert())在本地处理它。
Service class:服务等级:
public class FileService {
@Inject
private S3Utils s3utils;
private ConversionUtils conversionUtils = new ConversionUtils();
public void processFile() {
List<S3ObjectSummary> files = s3utils.getAllFiles();
List<S3Object> unprocessedFiles = s3utils.getUnprocessedFiles(files);
for(S3Object file: unprocessedFiles) {
InputStream content = file.getObjectContent();
List<Record> records = conversionUtils.convert(content); //Exception thrown here
}
}
}
S3Utils class: S3Utils 类:
@Component
public class S3utils {
@Inject AmazonS3 amazonS3;
public List<S3ObjectSummary> getAllFiles() {
ListObjectsV2Request request = new ListObjectsV2Request().withBucketName('something').withPrefix('some_prefix');
ListObjectsV2Result result = amazonS3.listObjectsV2(request);;
return result.getObjectSummaries();
}
public List<S3Object> getUnprocessedFiles(List<S3ObjectSummary> files) {
//do some filtering here
List<S3Object> unprocessedFiles = new ArrayList<>();
for (S3ObjectSummary summary : filteredSummaries) {
S3Object s3Object = amazonS3.getObject(new GetObjectRequest(summary.getBucketName(), summary.getKey()));
unprocessedFiles.add(S3Object);
}
return unprocessedFiles;
}
}
Config class:配置类:
@Configuration
public class Config {
@Bean
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard().withCredentials(new DefaultAWSCredentialsProviderChain()).build();
}
}
I've read some other threads with similar errors where the issue was that the AmazonS3 client was being garbage collected, and therefore closing the stream, but I'm wondering if that's the case here.我读过其他一些有类似错误的线程,其中问题是 AmazonS3 客户端被垃圾收集,因此关闭了流,但我想知道这里是否是这种情况。 Any ideas on what the issue is?关于问题是什么的任何想法? Thanks.谢谢。
I was able to overcome this by setting the ClientConfig to keep the connection alive我能够通过设置 ClientConfig 来保持连接活跃来克服这个问题
AmazonS3ClientBuilder
.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withClientConfiguration(new ClientConfiguration())
.withTcpKeepAlive(true)
.build();
I ran into same issue with aws sdk 1.X .我遇到了与 aws sdk 1.X相同的问题。
Strange thing here is, it's working fine in local machine and Connection reset while application runs on server.奇怪的是,当应用程序在服务器上运行时,它在本地机器和连接重置中工作正常。
Tried with withTcpKeepAlive(true)
, but result was same ie,尝试withTcpKeepAlive(true)
,但结果是一样的,即,
Java.net.SocketException: Connection reset Java.net.SocketException:连接重置
I tried withRange(startPosition,endPosition)
until I read all data and it worked.我尝试了withRange(startPosition,endPosition)
直到我读取了所有数据并且它起作用了。
在我将 s3 代理升级到 1.6.0 并将 aws-java-sdk-s3 升级到 1.1.285 后,连接重置问题得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.