AWS S3-带输入流的分段上传

时间:2019-03-31 14:44:10

标签: java amazon-web-services amazon-s3

我正在尝试将输入流作为分段上传的形式上传到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);
    }

0 个答案:

没有答案