@Test public void testDeleteMultipleObjects() throws Exception { String blobName = "foo"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); DeleteObjectsRequest request = new DeleteObjectsRequest(containerName) .withKeys(blobName); // without quiet client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); DeleteObjectsResult result = client.deleteObjects(request); assertThat(result.getDeletedObjects()).hasSize(1); assertThat(result.getDeletedObjects().iterator().next().getKey()) .isEqualTo(blobName); // with quiet client.putObject(containerName, blobName, BYTE_SOURCE.openStream(), metadata); result = client.deleteObjects(request.withQuiet(true)); assertThat(result.getDeletedObjects()).isEmpty(); }
/** * Tests if an object can be deleted */ @Test public void shouldBatchDeleteObjects() { final File uploadFile1 = new File(UPLOAD_FILE_NAME); final File uploadFile2 = new File(UPLOAD_FILE_NAME); final File uploadFile3 = new File(UPLOAD_FILE_NAME); final File uploadFile4 = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); s3Client .putObject(new PutObjectRequest(BUCKET_NAME, "1_" + UPLOAD_FILE_NAME, uploadFile1)); s3Client .putObject(new PutObjectRequest(BUCKET_NAME, "2_" + UPLOAD_FILE_NAME, uploadFile2)); s3Client .putObject(new PutObjectRequest(BUCKET_NAME, "3_" + UPLOAD_FILE_NAME, uploadFile3)); final DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(BUCKET_NAME); final List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(); keys.add(new DeleteObjectsRequest.KeyVersion("1_" + UPLOAD_FILE_NAME)); keys.add(new DeleteObjectsRequest.KeyVersion("2_" + UPLOAD_FILE_NAME)); keys.add(new DeleteObjectsRequest.KeyVersion("3_" + UPLOAD_FILE_NAME)); keys.add(new DeleteObjectsRequest.KeyVersion("4_" + UPLOAD_FILE_NAME)); multiObjectDeleteRequest.setKeys(keys); final DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest); assertThat("Response should contain 4 entries", delObjRes.getDeletedObjects().size(), is(4)); thrown.expect(AmazonS3Exception.class); thrown.expectMessage(containsString("Status Code: 406")); s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) { LOGGER.debug("deleteObjects(): deleteObjectRequest.getBucketName() = " + deleteObjectsRequest.getBucketName() + ", deleteObjectRequest.getKeys() = " + deleteObjectsRequest.getKeys()); List<DeletedObject> deletedObjects = new ArrayList<>(); MockS3Bucket mockS3Bucket = mockS3Buckets.get(deleteObjectsRequest.getBucketName()); for (KeyVersion keyVersion : deleteObjectsRequest.getKeys()) { String s3ObjectKey = keyVersion.getKey(); String s3ObjectVersion = keyVersion.getVersion(); String s3ObjectKeyVersion = s3ObjectKey + (s3ObjectVersion != null ? s3ObjectVersion : ""); mockS3Bucket.getObjects().remove(s3ObjectKey); if (mockS3Bucket.getVersions().remove(s3ObjectKeyVersion) != null) { DeletedObject deletedObject = new DeletedObject(); deletedObject.setKey(s3ObjectKey); deletedObject.setVersionId(s3ObjectVersion); deletedObjects.add(deletedObject); } } return new DeleteObjectsResult(deletedObjects); }
@Override public DeleteObjectsResult deleteObjects(final String bucketName, final List<KeyVersion> keys) throws AmazonClientException, AmazonServiceException { LOGGER.info("deleteObjects invoked, bucketName: {}, keys: {}", bucketName, keys); final DeleteObjectsRequest deleteObjReq = new DeleteObjectsRequest(bucketName); deleteObjReq.setKeys(keys); return s3client.deleteObjects(deleteObjReq); }
/** * Test method for {@link com.github.abhinavmishra14.aws.s3.service.AwsS3IamService#deleteObjects(java.lang.String,java.util.List)}. * * @throws Exception the exception */ @Test public void testDeleteObjects() throws Exception { List<KeyVersion> keys = new ArrayList<KeyVersion>(); KeyVersion key1= new KeyVersion(AWSUtilConstants.SAMPLE_FILE_NAME); KeyVersion key2= new KeyVersion("TestPutObject2.txt"); keys.add(key1); keys.add(key2); awsS3IamService.createBucket(AWS_S3_BUCKET);//create bucket for test DeleteObjectsResult delResp = awsS3IamService.deleteObjects(AWS_S3_BUCKET,keys); assertNotNull(delResp); }
@Override public boolean deleteEntities(String bucketName) { LOG.info("Deletes multiple objects in a bucket " + bucketName + " from Amazon S3"); List<S3ObjectSummary> s3ObjectSummaries = findEntityByBucket(bucketName); if (s3ObjectSummaries == null || s3ObjectSummaries.isEmpty()) { return false; } // Provide a list of object keys and versions. List<KeyVersion> keyVersions = new ArrayList<KeyVersion>(); for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) { keyVersions.add( new KeyVersion(s3ObjectSummary.getKey())); } try { DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName) .withKeys(keyVersions); DeleteObjectsResult deleteObjectsResult = amazonS3Client.deleteObjects(deleteObjectsRequest); if (deleteObjectsResult != null) { LOG.info("Successfully deleted all the " + deleteObjectsResult.getDeletedObjects().size() + " items.\n"); return true; } } catch (AmazonServiceException ase) { LOG.warn(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.warn(ace.getMessage(), ace); } return false; }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException, AmazonServiceException { return delegate.deleteObjects(deleteObjectsRequest); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) { deleteObjectsRequest = beforeClientExecution(deleteObjectsRequest); Request<DeleteObjectsRequest> request = createRequest(deleteObjectsRequest.getBucketName(), null, deleteObjectsRequest, HttpMethodName.POST); request.addParameter("delete", null); if ( deleteObjectsRequest.getMfa() != null ) { populateRequestWithMfaDetails(request, deleteObjectsRequest.getMfa()); } populateRequesterPaysHeader(request, deleteObjectsRequest.isRequesterPays()); byte[] content = new MultiObjectDeleteXmlFactory().convertToXmlByteArray(deleteObjectsRequest); request.addHeader("Content-Length", String.valueOf(content.length)); request.addHeader("Content-Type", "application/xml"); request.setContent(new ByteArrayInputStream(content)); try { byte[] md5 = Md5Utils.computeMD5Hash(content); String md5Base64 = BinaryUtils.toBase64(md5); request.addHeader("Content-MD5", md5Base64); } catch ( Exception e ) { throw new SdkClientException("Couldn't compute md5 sum", e); } @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<DeleteObjectsResponse> responseHandler = new ResponseHeaderHandlerChain<DeleteObjectsResponse>( new Unmarshallers.DeleteObjectsResultUnmarshaller(), new S3RequesterChargedHeaderHandler<DeleteObjectsResponse>()); DeleteObjectsResponse response = invoke(request, responseHandler, deleteObjectsRequest.getBucketName(), null); /* * If the result was only partially successful, throw an exception */ if ( !response.getErrors().isEmpty() ) { Map<String, String> headers = responseHandler.getResponseHeaders(); MultiObjectDeleteException ex = new MultiObjectDeleteException( response.getErrors(), response.getDeletedObjects()); ex.setStatusCode(200); ex.setRequestId(headers.get(Headers.REQUEST_ID)); ex.setExtendedRequestId(headers.get(Headers.EXTENDED_REQUEST_ID)); ex.setCloudFrontId(headers.get(Headers.CLOUD_FRONT_ID)); throw ex; } DeleteObjectsResult result = new DeleteObjectsResult(response.getDeletedObjects(), response.isRequesterCharged()); return result; }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws SdkClientException, AmazonServiceException { return call(() -> getDelegate().deleteObjects(deleteObjectsRequest)); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) { return s3Client.deleteObjects(deleteObjectsRequest); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException { return null; }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
/** * Deletes the specified S3 objects in the specified S3 bucket. * * @param deleteObjectsRequest the request object containing all the options for deleting multiple objects in a specified bucket * @param s3Client the {@link AmazonS3} implementation to use * * @return the successful response to the delete object request */ public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client);
/** * Delete objects. * * @param bucketName the bucket name * @param keys the keys * @return the delete objects result * @throws AmazonClientException the amazon client exception * @throws AmazonServiceException the amazon service exception */ DeleteObjectsResult deleteObjects(final String bucketName, final List<KeyVersion> keys) throws AmazonClientException, AmazonServiceException;