AWS Code Sample
Catalog

LowLevelAbortMultipartUpload.java

LowLevelAbortMultipartUpload.java demonstrates how to abort an in-progress multipart upload.

/** * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; 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; public class LowLevelAbortMultipartUpload { public static void main(String[] args) { String clientRegion = "*** Client 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(); } } }

Sample Details

Service: s3

Last tested: 2019-01-28

Author: AWS

Type: full-example

On this page: