Amazon S3多部分上传使用低级java API只上传单个部分



我已经编写了以下代码,使用多部分上传(每个部分都大于5MB(将集合字符串上传到S3,但当我从S3下载回来时,它只包含一部分的字符串。

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class LowLevelMultipartUpload {
private List<PartETag> partETags = new ArrayList<>();
private InitiateMultipartUploadResult initResponse;
AmazonS3 s3Client; String bucketName; String keyName;
public LowLevelMultipartUpload(AmazonS3 s3Client, String bucketName, String keyName) {
this.s3Client = s3Client;
this.bucketName = bucketName;
this.keyName = keyName;
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
initResponse = s3Client.initiateMultipartUpload(initRequest);
}
public void uploadPart(int partNumber, InputStream is, ObjectMetadata objectMetadata, boolean isFinalPart) throws IOException {
// Create the request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey(keyName)
.withUploadId(initResponse.getUploadId())
.withPartNumber(partNumber)
.withInputStream(is)
.withPartSize(is.available())
.withObjectMetadata(objectMetadata);
if(isFinalPart) uploadRequest.withLastPart(true);
// Upload the part and add the response's ETag to our list.
UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest);
partETags.add(uploadResult.getPartETag());
}
public void completeUpload() {
// Complete the multipart upload.
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
initResponse.getUploadId(), partETags);
s3Client.completeMultipartUpload(compRequest);
}
}

我按以下顺序调用上面的代码。

  1. 创建LowLevelMultipartUpload对象
  2. 上传循环中的零件
  3. 呼叫完成上传

上面的代码没有问题,工作正常。

相关内容

  • 没有找到相关文章

最新更新