Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

中止分段上传

您可以通过调用 AmazonS3Client.abortMultipartUpload() 方法中止正在进行的分段上传。此方法将删除所有上传到 Amazon S3 的分段并释放资源。您应提供上传 ID、存储桶名称和键名称。

以下示例介绍如何使用低级别 Java API 中止分段上传。

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(); } } }

注意

您可以中止在特定时间之前启动的正在进行的分段上传,而不是单独中止分段上传。对于中止您启动但未完成或已中止的分段上传,此清除操作很有用。有关更多信息,请参阅 中止分段上传