또는와 AbortMultipartUploadAWS SDK 함께 사용 CLI - AWS SDK 코드 예제

AWS 문서 예제 리포지토리에서 더 많은 SDK GitHub AWS SDK 예제를 사용할 수 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

또는와 AbortMultipartUploadAWS SDK 함께 사용 CLI

다음 코드 예제는 AbortMultipartUpload의 사용 방법을 보여 줍니다.

작업 예시는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

C++
SDK C++용
참고

더 많은 기능이 있습니다 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 자세한 내용은 AWS SDK for C++ API 참조AbortMultipartUpload의 섹션을 참조하세요.

CLI
AWS CLI

지정된 멀티파트 업로드를 중단하려면

다음 abort-multipart-upload 명령은 my-bucket 버킷의 multipart/01 키에 대한 멀티파트 업로드를 중단합니다.

aws s3api abort-multipart-upload \ --bucket my-bucket \ --key multipart/01 \ --upload-id dfRtDYU0WWCCcH43C3WFbkRONycyCpTJJvxu2i5GYkZljF.Yxwh6XG7WfS2vC4to6HiV6Yjlx.cph0gtNBtJ8P3URCSbB7rjxI5iEwVDmgaXZOGgkk5nVTW16HOQ5l0R

이 명령에 필요한 업로드 ID는 create-multipart-upload에서 출력되며 list-multipart-uploads를 사용하여 검색할 수도 있습니다.

Java
SDK Java 2.x용
참고

더 많은 기능이 있습니다 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 자세한 내용은 AWS SDK for Java 2.x API 참조AbortMultipartUpload의 섹션을 참조하세요.

PowerShell
용 도구 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의 섹션을 참조하세요.