Abort a multipart upload - Amazon Simple Storage Service

Abort a multipart upload

You can stop an in-progress multipart upload by calling the AmazonS3Client.abortMultipartUpload() method. This method deletes all parts that were uploaded to Amazon S3 and frees the resources. You provide the upload ID, bucket name, and key name.

The following example shows how to stop multipart uploads using the low-level Java API.

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.AbortMultipartUploadRequest; import com.amazonaws.services.s3.model.ListMultipartUploadsRequest; import com.amazonaws.services.s3.model.MultipartUpload; import com.amazonaws.services.s3.model.MultipartUploadListing; import java.util.List; public class LowLevelAbortMultipartUpload { public static void main(String[] args) { Regions clientRegion = Regions.DEFAULT_REGION; String bucketName = "*** Bucket name ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); // Find all in-progress multipart uploads. ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); MultipartUploadListing multipartUploadListing = s3Client.listMultipartUploads(allMultipartUploadsRequest); List<MultipartUpload> uploads = multipartUploadListing.getMultipartUploads(); System.out.println("Before deletions, " + uploads.size() + " multipart uploads in progress."); // Abort each upload. for (MultipartUpload u : uploads) { System.out.println("Upload in progress: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, u.getKey(), u.getUploadId())); System.out.println("Upload deleted: Key = \"" + u.getKey() + "\", id = " + u.getUploadId()); } // Verify that all in-progress multipart uploads have been aborted. multipartUploadListing = s3Client.listMultipartUploads(allMultipartUploadsRequest); uploads = multipartUploadListing.getMultipartUploads(); System.out.println("After aborting uploads, " + uploads.size() + " multipart uploads in progress."); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

Instead of stopping multipart uploads individually, you can stop all of your in-progress multipart uploads that were initiated before a specific time. This clean-up operation is useful for aborting multipart uploads that you initiated but that didn't complete or were aborted. For more information, see Stop multipart uploads.