@Test public void deleteShouldRemoveItemsFromBucket() { // given PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket("someBucket") .key("someObject") .build(); RequestBody requestBody = RequestBody.fromString("content"); testee.putObject(putObjectRequest, requestBody); testee.deleteObjects(DeleteObjectsRequest.builder().bucket("someBucket").delete(Delete.builder().objects (ObjectIdentifier.builder().key("someObject").build()).build()).build()); // when ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket("someBucket") .build(); ListObjectsV2Response listObjectsV2Response = testee.listObjectsV2(listObjectsV2Request); //then assertThat(listObjectsV2Response.contents().size(), is(0)); }
/** * <p> * This operation enables you to delete multiple objects from a bucket using a single HTTP request. You may specify * up to 1000 keys. * </p> * <br/> * <p> * This is a convenience which creates an instance of the {@link DeleteObjectsRequest.Builder} avoiding the need to * create one manually via {@link DeleteObjectsRequest#builder()} * </p> * * @param deleteObjectsRequest * A {@link Consumer} that will call methods on {@link DeleteObjectsRequest.Builder} to create a request. * @return A Java Future containing the result of the DeleteObjects operation returned by the service.<br/> * The CompletableFuture returned by this method can be completed exceptionally with the following * exceptions. * <ul> * <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client). * Can be used for catch all scenarios.</li> * <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get * credentials, etc.</li> * <li>S3Exception Base class for all service exceptions. Unknown exceptions will be thrown as an instance * of this type.</li> * </ul> * @sample S3AsyncClient.DeleteObjects */ default CompletableFuture<DeleteObjectsResponse> deleteObjects(Consumer<DeleteObjectsRequest.Builder> deleteObjectsRequest) { return deleteObjects(DeleteObjectsRequest.builder().applyMutation(deleteObjectsRequest).build()); }
/** * <p> * This operation enables you to delete multiple objects from a bucket using a single HTTP request. You may specify * up to 1000 keys. * </p> * <br/> * <p> * This is a convenience which creates an instance of the {@link DeleteObjectsRequest.Builder} avoiding the need to * create one manually via {@link DeleteObjectsRequest#builder()} * </p> * * @param deleteObjectsRequest * A {@link Consumer} that will call methods on {@link DeleteObjectsRequest.Builder} to create a request. * @return Result of the DeleteObjects operation returned by the service. * @throws SdkException * Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for * catch all scenarios. * @throws SdkClientException * If any client side error occurs such as an IO related failure, failure to get credentials, etc. * @throws S3Exception * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample S3Client.DeleteObjects */ default DeleteObjectsResponse deleteObjects(Consumer<DeleteObjectsRequest.Builder> deleteObjectsRequest) throws AwsServiceException, SdkClientException, S3Exception { return deleteObjects(DeleteObjectsRequest.builder().applyMutation(deleteObjectsRequest).build()); }
public void deleteAllObjectsWithPrefixInBucket(final String bucketName, final String prefix) { final ListObjectsV2Response listObjectResponse = s3Client.listObjectsV2(ListObjectsV2Request.builder().bucket(bucketName).build()); if (listObjectResponse.keyCount() > 0) { DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder() .bucket(bucketName) .delete(builder() .objects(convertS3ObjectsToObjectIdentifiers(listObjectResponse, prefix)) .build()) .build(); final DeleteObjectsResponse deleteObjectsResponse = s3Client.deleteObjects(deleteObjectsRequest); LOG.debug("deleteAllObjectsWithPrefixInBucket in bucket {} with prefix {}: {}", bucketName, prefix, deleteObjectsResponse); } else { LOG.debug("deleteAllObjectsWithPrefixInBucket listObjects found no keys in bucket {} with prefix {}: {}", bucketName, prefix, listObjectResponse); } }
/** * Sets the value of the Delete property for this object. * * This is a convenience that creates an instance of the {@link Delete.Builder} avoiding the need to create one * manually via {@link Delete#builder()}. * * When the {@link Consumer} completes, {@link Delete.Builder#build()} is called immediately and its result is * passed to {@link #delete(Delete)}. * * @param delete * a consumer that will call methods on {@link Delete.Builder} * @return Returns a reference to this object so that method calls can be chained together. * @see #delete(Delete) */ default Builder delete(Consumer<Delete.Builder> delete) { return delete(Delete.builder().applyMutation(delete).build()); }