There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CompleteMultipartUpload
with an AWS SDK
The following code example shows how to use CompleteMultipartUpload
.
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Complete a multipart upload in a directory bucket.
import com.example.s3.util.S3DirectoryBucketUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.S3Exception; import java.io.IOException; import java.nio.file.Path; import java.util.List; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; /** * This method completes the multipart upload request by collating all the * upload parts. * * @param s3Client The S3 client used to interact with S3 * @param bucketName The name of the directory bucket * @param objectKey The key (name) of the object to be uploaded * @param uploadId The upload ID used to track the multipart upload * @param uploadParts The list of completed parts * @return True if the multipart upload is successfully completed, false * otherwise */ public static boolean completeDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey, String uploadId, List<CompletedPart> uploadParts) { try { CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() .parts(uploadParts) .build(); CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() .bucket(bucketName) .key(objectKey) .uploadId(uploadId) .multipartUpload(completedMultipartUpload) .build(); CompleteMultipartUploadResponse response = s3Client.completeMultipartUpload(completeMultipartUploadRequest); logger.info("Multipart upload completed. ETag: {}", response.eTag()); return true; } catch (S3Exception e) { logger.error("Failed to complete multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode(), e); return false; } }
-
For API details, see CompleteMultipartUpload in AWS SDK for Java 2.x API Reference.
-
AbortMultipartUpload
CopyObject