文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AbortMultipartUpload
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 AbortMultipartUpload
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- C++
-
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Abort a multipart upload to an S3 bucket. /*! \param bucket: The name of the S3 bucket where the object will be uploaded. \param key: The unique identifier (key) for the object within the S3 bucket. \param uploadID: An upload ID string. \param client: The S3 client instance used to perform the upload operation. \return bool: Function succeeded. */ bool AwsDoc::S3::abortMultipartUpload(const Aws::String &bucket, const Aws::String &key, const Aws::String &uploadID, const Aws::S3::S3Client &client) { Aws::S3::Model::AbortMultipartUploadRequest request; request.SetBucket(bucket); request.SetKey(key); request.SetUploadId(uploadID); Aws::S3::Model::AbortMultipartUploadOutcome outcome = client.AbortMultipartUpload(request); if (outcome.IsSuccess()) { std::cout << "Multipart upload aborted." << std::endl; } else { std::cerr << "Error aborting multipart upload: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 AbortMultipartUpload AWS SDK for C++ 參考中的 API。
-
- CLI
-
- AWS CLI
-
若要中止指定的分段上傳
下列
abort-multipart-upload
命令會中止儲存貯體multipart/01
中金鑰的分段上傳my-bucket
。aws s3api abort-multipart-upload \ --bucket
my-bucket
\ --keymultipart/01
\ --upload-iddfRtDYU0WWCCcH43C3WFbkRONycyCpTJJvxu2i5GYkZljF.Yxwh6XG7WfS2vC4to6HiV6Yjlx.cph0gtNBtJ8P3URCSbB7rjxI5iEwVDmgaXZOGgkk5nVTW16HOQ5l0R
此命令所需的上傳 ID 由 輸出,
create-multipart-upload
也可以使用 擷取list-multipart-uploads
。-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 AbortMultipartUpload
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.exception.SdkException; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.AbortMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.LifecycleRule; import software.amazon.awssdk.services.s3.model.ListMultipartUploadsRequest; import software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse; import software.amazon.awssdk.services.s3.model.MultipartUpload; import software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.model.UploadPartResponse; import software.amazon.awssdk.services.s3.waiters.S3Waiter; import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.utils.builder.SdkBuilder; import java.io.IOException; import java.io.RandomAccessFile; import java.net.URISyntaxException; import java.net.URL; import java.nio.ByteBuffer; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.UUID; import static software.amazon.awssdk.transfer.s3.SizeConstant.KB; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AbortMultipartUploadExamples { static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. static final String key = UUID.randomUUID().toString(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); static final S3Client s3Client = S3Client.create(); private static final Logger logger = LoggerFactory.getLogger(AbortMultipartUploadExamples.class); private static String accountId = getAccountId(); public static void main(String[] args) { doAbortIncompleteMultipartUploadsFromList(); doAbortMultipartUploadUsingUploadId(); doAbortIncompleteMultipartUploadsOlderThan(); doAbortMultipartUploadsUsingLifecycleConfig(); } // A wrapper method that sets up the multipart upload environment for abortIncompleteMultipartUploadsFromList(). public static void doAbortIncompleteMultipartUploadsFromList() { createBucket(); initiateAndInterruptMultiPartUpload("uploadThread"); abortIncompleteMultipartUploadsFromList(); deleteResources(); } /** * Aborts all incomplete multipart uploads from the specified S3 bucket. * <p> * This method retrieves a list of all incomplete multipart uploads in the specified S3 bucket, * and then aborts each of those uploads. */ public static void abortIncompleteMultipartUploadsFromList() { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() .bucket(bucketName) .build(); ListMultipartUploadsResponse response = s3Client.listMultipartUploads(listMultipartUploadsRequest); List<MultipartUpload> uploads = response.uploads(); AbortMultipartUploadRequest abortMultipartUploadRequest; for (MultipartUpload upload : uploads) { abortMultipartUploadRequest = AbortMultipartUploadRequest.builder() .bucket(bucketName) .key(upload.key()) .expectedBucketOwner(accountId) .uploadId(upload.uploadId()) .build(); AbortMultipartUploadResponse abortMultipartUploadResponse = s3Client.abortMultipartUpload(abortMultipartUploadRequest); if (abortMultipartUploadResponse.sdkHttpResponse().isSuccessful()) { logger.info("Upload ID [{}] to bucket [{}] successfully aborted.", upload.uploadId(), bucketName); } } } // A wrapper method that sets up the multipart upload environment for abortIncompleteMultipartUploadsOlderThan(). static void doAbortIncompleteMultipartUploadsOlderThan() { createBucket(); Instant secondUploadInstant = initiateAndInterruptTwoUploads(); abortIncompleteMultipartUploadsOlderThan(secondUploadInstant); deleteResources(); } static void abortIncompleteMultipartUploadsOlderThan(Instant pointInTime) { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() .bucket(bucketName) .build(); ListMultipartUploadsResponse response = s3Client.listMultipartUploads(listMultipartUploadsRequest); List<MultipartUpload> uploads = response.uploads(); AbortMultipartUploadRequest abortMultipartUploadRequest; for (MultipartUpload upload : uploads) { logger.info("Found multipartUpload with upload ID [{}], initiated [{}]", upload.uploadId(), upload.initiated()); if (upload.initiated().isBefore(pointInTime)) { abortMultipartUploadRequest = AbortMultipartUploadRequest.builder() .bucket(bucketName) .key(upload.key()) .expectedBucketOwner(accountId) .uploadId(upload.uploadId()) .build(); AbortMultipartUploadResponse abortMultipartUploadResponse = s3Client.abortMultipartUpload(abortMultipartUploadRequest); if (abortMultipartUploadResponse.sdkHttpResponse().isSuccessful()) { logger.info("Upload ID [{}] to bucket [{}] successfully aborted.", upload.uploadId(), bucketName); } } } } // A wrapper method that sets up the multipart upload environment for abortMultipartUploadUsingUploadId(). static void doAbortMultipartUploadUsingUploadId() { createBucket(); try { abortMultipartUploadUsingUploadId(); } catch (S3Exception e) { logger.error(e.getMessage()); } finally { deleteResources(); } } static void abortMultipartUploadUsingUploadId() { String uploadId = startUploadReturningUploadId(); AbortMultipartUploadResponse response = s3Client.abortMultipartUpload(b -> b .uploadId(uploadId) .bucket(bucketName) .key(key)); if (response.sdkHttpResponse().isSuccessful()) { logger.info("Upload ID [{}] to bucket [{}] successfully aborted.", uploadId, bucketName); } } // A wrapper method that sets up the multipart upload environment for abortMultipartUploadsUsingLifecycleConfig(). static void doAbortMultipartUploadsUsingLifecycleConfig() { createBucket(); try { abortMultipartUploadsUsingLifecycleConfig(); } catch (S3Exception e) { logger.error(e.getMessage()); } finally { deleteResources(); } } static void abortMultipartUploadsUsingLifecycleConfig() { Collection<LifecycleRule> lifeCycleRules = List.of(LifecycleRule.builder() .abortIncompleteMultipartUpload(b -> b. daysAfterInitiation(7)) .status("Enabled") .filter(SdkBuilder::build) // Filter element is required. .build()); // If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body. PutBucketLifecycleConfigurationResponse response = s3Client.putBucketLifecycleConfiguration(b -> b .bucket(bucketName) .lifecycleConfiguration(b1 -> b1.rules(lifeCycleRules))); if (response.sdkHttpResponse().isSuccessful()) { logger.info("Rule to abort incomplete multipart uploads added to bucket."); } else { logger.error("Unsuccessfully applied rule. HTTP status code is [{}]", response.sdkHttpResponse().statusCode()); } } /************************ Multipart upload methods ***********************/ static void initiateAndInterruptMultiPartUpload(String threadName) { Runnable upload = () -> { try { AbortMultipartUploadExamples.doMultipartUpload(); } catch (SdkException e) { logger.error(e.getMessage()); } }; Thread uploadThread = new Thread(upload, threadName); uploadThread.start(); try { Thread.sleep(Duration.ofSeconds(1).toMillis()); // Give the multipart upload time to register. } catch (InterruptedException e) { logger.error(e.getMessage()); } uploadThread.interrupt(); } static Instant initiateAndInterruptTwoUploads() { Instant firstUploadInstant = Instant.now(); initiateAndInterruptMultiPartUpload("uploadThread1"); try { Thread.sleep(Duration.ofSeconds(5).toMillis()); } catch (InterruptedException e) { logger.error(e.getMessage()); } Instant secondUploadInstant = Instant.now(); initiateAndInterruptMultiPartUpload("uploadThread2"); return secondUploadInstant; } static void doMultipartUpload() { String uploadId = step1CreateMultipartUpload(); List<CompletedPart> completedParts = step2UploadParts(uploadId); step3CompleteMultipartUpload(uploadId, completedParts); } static String step1CreateMultipartUpload() { CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b .bucket(bucketName) .key(key)); return createMultipartUploadResponse.uploadId(); } static List<CompletedPart> step2UploadParts(String uploadId) { int partNumber = 1; List<CompletedPart> completedParts = new ArrayList<>(); ByteBuffer bb = ByteBuffer.allocate(Long.valueOf(1024 * KB).intValue()); try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) { long fileSize = file.length(); long position = 0; while (position < fileSize) { file.seek(position); long read = file.getChannel().read(bb); bb.flip(); // Swap position and limit before reading from the buffer. UploadPartRequest uploadPartRequest = UploadPartRequest.builder() .bucket(bucketName) .key(key) .uploadId(uploadId) .partNumber(partNumber) .build(); UploadPartResponse partResponse = s3Client.uploadPart( uploadPartRequest, RequestBody.fromByteBuffer(bb)); CompletedPart part = CompletedPart.builder() .partNumber(partNumber) .eTag(partResponse.eTag()) .build(); completedParts.add(part); logger.info("Part {} upload", partNumber); bb.clear(); position += read; partNumber++; } } catch (IOException | S3Exception e) { logger.error(e.getMessage()); return null; } return completedParts; } static void step3CompleteMultipartUpload(String uploadId, List<CompletedPart> completedParts) { s3Client.completeMultipartUpload(b -> b .bucket(bucketName) .key(key) .uploadId(uploadId) .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); } static String startUploadReturningUploadId() { String uploadId = step1CreateMultipartUpload(); doMultipartUploadWithUploadId(uploadId); return uploadId; } static void doMultipartUploadWithUploadId(String uploadId) { new Thread(() -> { try { List<CompletedPart> completedParts = step2UploadParts(uploadId); step3CompleteMultipartUpload(uploadId, completedParts); } catch (SdkException e) { logger.error(e.getMessage()); } }, "upload thread").start(); try { Thread.sleep(Duration.ofSeconds(2L).toMillis()); } catch (InterruptedException e) { logger.error(e.getMessage()); System.exit(1); } } /************************* Resource handling methods ************************/ static void createBucket() { logger.info("Creating bucket: [{}]", bucketName); s3Client.createBucket(b -> b.bucket(bucketName)); try (S3Waiter s3Waiter = s3Client.waiter()) { s3Waiter.waitUntilBucketExists(b -> b.bucket(bucketName)); } logger.info("Bucket created."); } static void deleteResources() { logger.info("Deleting resources ..."); s3Client.deleteObject(b -> b.bucket(bucketName).key(key)); s3Client.deleteBucket(b -> b.bucket(bucketName)); try (S3Waiter s3Waiter = s3Client.waiter()) { s3Waiter.waitUntilBucketNotExists(b -> b.bucket(bucketName)); } logger.info("Resources deleted."); } private static String getAccountId() { try (StsClient stsClient = StsClient.create()) { return stsClient.getCallerIdentity().account(); } } static String getFullFilePath(String filePath) { URL uploadDirectoryURL = PerformMultiPartUpload.class.getResource(filePath); String fullFilePath; try { fullFilePath = Objects.requireNonNull(uploadDirectoryURL).toURI().getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); } return fullFilePath; } }
-
如需 API 詳細資訊,請參閱 AbortMultipartUpload AWS SDK for Java 2.x 參考中的 API。
-
- PowerShell
-
- for PowerShell 工具
-
範例 1:此命令會中止在 5 天前建立的分段上傳。
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -DaysBefore 5
範例 2:此命令會中止在 2014 年 1 月 2 日之前建立的分段上傳。
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "Thursday, January 02, 2014"
範例 3:此命令會中止在 2014 年 1 月 2 日 10:45:37 之前建立的分段上傳。
Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "2014/01/02 10:45:37"
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 AbortMultipartUpload。
-