我正在尝试将输入流作为分段上传的形式上传到S3中(以实现重试机制)。如果在上传或转换过程中发生任何错误,我需要从这一点恢复。当我尝试上传时,它将返回类似Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.
public static void main(String[] args) throws IOException {
PartETag partETag = uploadObjectAsMultipart(s3Client, bucketName, initResponse.getUploadId(), stringBuilder, 1, fileName, file);
combineMultiPartUpload(s3Client, bucketName, fileName, initResponse.getUploadId(), new ArrayList<PartETag>(){{add(partETag);}});
}
public static PartETag uploadObjectAsMultipart(AmazonS3Client s3Client, String bucketName, String uploadId, StringBuilder stringBuilder, int partNumber, String fileName, File file) {
UploadPartRequest uploadRequest =
new UploadPartRequest()
.withBucketName(bucketName)
.withUploadId(uploadId)
.withKey(fileName)
.withPartNumber(partNumber)
.withInputStream(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)))
.withPartSize(5 * 1024 * 1024);
return s3Client.uploadPart(uploadRequest).getPartETag();
}
public static void combineMultiPartUpload(AmazonS3Client s3Client, String bucketName, String fileName, String uploadId, List<PartETag> partETags) {
CompleteMultipartUploadRequest compRequest =
new CompleteMultipartUploadRequest(bucketName,
fileName,
uploadId,
partETags);
s3Client.completeMultipartUpload(compRequest);
}