View a markdown version of this page

Use AbortMultipartUploadcom um AWS SDK ou CLI - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use AbortMultipartUploadcom um AWS SDK ou CLI

Os exemplos de código a seguir mostram como usar o AbortMultipartUpload.

Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação em contexto nos seguintes exemplos de código:

C++
SDK para C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

//! 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(); }
CLI
AWS CLI

Para interromper um carregamento fracionado especificado

O comando abort-multipart-upload a seguir interrompe um carregamento fracionado da chave multipart/01 no bucket amzn-s3-demo-bucket.

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

O ID de upload exigido por esse comando é a saída de create-multipart-upload e também pode ser recuperado com list-multipart-uploads.

Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

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 S3Client s3Client = S3Client.create(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); private static final Logger logger = LoggerFactory.getLogger(AbortMultipartUploadExamples.class); private static final String accountId = getAccountId(); public static void main(String[] args) { String bucketName = "amzn-s3-demo-bucket"; // Replace with your bucket name. String key = UUID.randomUUID().toString(); createBucket(bucketName); try { initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread"); abortIncompleteMultipartUploadsFromList(bucketName); abortMultipartUploadUsingUploadId(bucketName, key); abortMultipartUploadsUsingLifecycleConfig(bucketName); } catch (S3Exception e) { logger.error(e.getMessage()); } finally { deleteResources(bucketName, key); } } /** * Aborts all incomplete multipart uploads from the specified S3 bucket. * * @param bucketName the name of the S3 bucket */ public static void abortIncompleteMultipartUploadsFromList(String bucketName) { 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); } } } /** * Aborts incomplete multipart uploads older than the specified point in time. * * @param bucketName the name of the S3 bucket * @param pointInTime the cutoff time; uploads initiated before this are aborted */ static void abortIncompleteMultipartUploadsOlderThan(String bucketName, 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); } } } } /** * Aborts a multipart upload using the upload ID. * * @param bucketName the name of the S3 bucket * @param key the object key */ static void abortMultipartUploadUsingUploadId(String bucketName, String key) { String uploadId = startUploadReturningUploadId(bucketName, key); 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); } } /** * Configures a lifecycle rule to abort incomplete multipart uploads after 7 days. * * @param bucketName the name of the S3 bucket */ static void abortMultipartUploadsUsingLifecycleConfig(String bucketName) { Collection<LifecycleRule> lifeCycleRules = List.of(LifecycleRule.builder() .abortIncompleteMultipartUpload(b -> b. daysAfterInitiation(7)) .status("Enabled") .filter(SdkBuilder::build) // Filter element is required. .build()); 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 bucketName, String key, String threadName) { Runnable upload = () -> { try { doMultipartUpload(bucketName, key); } catch (SdkException e) { logger.error(e.getMessage()); } }; Thread uploadThread = new Thread(upload, threadName); uploadThread.start(); try { Thread.sleep(Duration.ofSeconds(1).toMillis()); } catch (InterruptedException e) { logger.error(e.getMessage()); } uploadThread.interrupt(); } static Instant initiateAndInterruptTwoUploads(String bucketName, String key) { initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread1"); try { Thread.sleep(Duration.ofSeconds(5).toMillis()); } catch (InterruptedException e) { logger.error(e.getMessage()); } Instant secondUploadInstant = Instant.now(); initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread2"); return secondUploadInstant; } static void doMultipartUpload(String bucketName, String key) { String uploadId = step1CreateMultipartUpload(bucketName, key); List<CompletedPart> completedParts = step2UploadParts(bucketName, key, uploadId); step3CompleteMultipartUpload(bucketName, key, uploadId, completedParts); } static String step1CreateMultipartUpload(String bucketName, String key) { CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b .bucket(bucketName) .key(key)); return createMultipartUploadResponse.uploadId(); } static List<CompletedPart> step2UploadParts(String bucketName, String key, 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(); 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 bucketName, String key, 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 bucketName, String key) { String uploadId = step1CreateMultipartUpload(bucketName, key); doMultipartUploadWithUploadId(bucketName, key, uploadId); return uploadId; } static void doMultipartUploadWithUploadId(String bucketName, String key, String uploadId) { new Thread(() -> { try { List<CompletedPart> completedParts = step2UploadParts(bucketName, key, uploadId); step3CompleteMultipartUpload(bucketName, key, 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(String bucketName) { 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(String bucketName, String key) { 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; } }
  • Para obter detalhes da API, consulte AbortMultipartUploada Referência AWS SDK for Java 2.x da API.

PowerShell
Ferramentas para PowerShell V4

Exemplo 1: este comando interrompe os carregamentos fracionados criados há mais de cinco dias.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -DaysBefore 5

Exemplo 2: este comando interrompe os carregamentos fracionados criados antes de 2 de janeiro de 2014.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "Thursday, January 02, 2014"

Exemplo 3: este comando interrompe os carregamentos fracionados criados antes de 2 de janeiro de 2014, às 10:45:37.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "2014/01/02 10:45:37"
  • Para obter detalhes da API, consulte AbortMultipartUploadem Referência de Ferramentas da AWS para PowerShell cmdlet (V4).

Ferramentas para PowerShell V5

Exemplo 1: este comando interrompe os carregamentos fracionados criados há mais de cinco dias.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -DaysBefore 5

Exemplo 2: este comando interrompe os carregamentos fracionados criados antes de 2 de janeiro de 2014.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "Thursday, January 02, 2014"

Exemplo 3: este comando interrompe os carregamentos fracionados criados antes de 2 de janeiro de 2014, às 10:45:37.

Remove-S3MultipartUpload -BucketName amzn-s3-demo-bucket -InitiatedDate "2014/01/02 10:45:37"
  • Para obter detalhes da API, consulte AbortMultipartUploadem Referência de Ferramentas da AWS para PowerShell cmdlet (V5).