/** * Converts the specified {@link DeleteObjectsRequest} object to an XML fragment that * can be sent to Amazon S3. * * @param rq * The {@link DeleteObjectsRequest} */ public byte[] convertToXmlByteArray(DeleteObjectsRequest rq) throws SdkClientException { XmlWriter xml = new XmlWriter(); xml.start("Delete"); if ( rq.getQuiet() ) { xml.start("Quiet").value("true").end(); } for (KeyVersion keyVersion : rq.getKeys()) { writeKeyVersion(xml, keyVersion); } xml.end(); return xml.getBytes(); }
@Override public void remove(String accessKey) throws Exception { LogUtils.debug(LOG_TAG, "Deleting file with access key: " + accessKey); AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain()); DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName); List<KeyVersion> keys = new ArrayList<KeyVersion>(); keys.add(new KeyVersion(accessKey)); keys.add(new KeyVersion(accessKey + "_key")); multiObjectDeleteRequest.setKeys(keys); s3Client.deleteObjects(multiObjectDeleteRequest); LogUtils.debug(LOG_TAG, "Deleted file with access key: " + accessKey); }
static void deletePrefix(AmazonS3 s3Client, String bucketName, String prefix) { List<DeleteObjectsRequest.KeyVersion> trashKeys = new ArrayList<>(); DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName); for (S3ObjectSummary summary: S3Objects.withPrefix(s3Client, bucketName, prefix)) { trashKeys.add(new DeleteObjectsRequest.KeyVersion(summary.getKey())); if (trashKeys.size() == BULK_DELETE_SIZE) { deleteObjectsRequest.setKeys(trashKeys); s3Client.deleteObjects(deleteObjectsRequest); trashKeys.clear(); } } if (!trashKeys.isEmpty()) { deleteObjectsRequest.setKeys(trashKeys); s3Client.deleteObjects(deleteObjectsRequest); } }
public void deleteAllKeysOnPartialPath(String path) { ObjectListing objectListing = s3Client.listObjects(s3Bucket, getFullPath(path)); if (objectListing.getObjectSummaries().isEmpty()) { return; } List<DeleteObjectsRequest.KeyVersion> keys = objectListing .getObjectSummaries() .stream() .map(objectSummary -> new DeleteObjectsRequest.KeyVersion(objectSummary.getKey())) .collect(Collectors.toList()); DeleteObjectsRequest request = new DeleteObjectsRequest(s3Bucket); request.setKeys(keys); s3Client.deleteObjects(request); }
public static void s3DeleteRecursively(AmazonS3 s3, String bucket, String prefix) throws Exception { ListObjectsRequest request = new ListObjectsRequest() .withBucketName(bucket) .withPrefix(prefix); while (true) { ObjectListing listing = s3.listObjects(request); String[] keys = listing.getObjectSummaries().stream().map(S3ObjectSummary::getKey).toArray(String[]::new); for (String key : keys) { logger.info("delete s3://{}/{}", bucket, key); } retryExecutor() .retryIf(e -> e instanceof AmazonServiceException) .run(() -> s3.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys))); if (listing.getNextMarker() == null) { break; } } }
public void deleteAll(String bucket) { logger.info("delete all from bucket, bucket={}", bucket); ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket)); while (listing != null) { List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(listing.getObjectSummaries().size()); for (S3ObjectSummary summary : listing.getObjectSummaries()) { String key = summary.getKey(); logger.info("add key to deletion batch, key={}", key); keys.add(new DeleteObjectsRequest.KeyVersion(key)); } if (!keys.isEmpty()) { logger.info("delete key batch"); s3.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys)); } if (!listing.isTruncated()) return; listing = s3.listNextBatchOfObjects(listing); } }
@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(); }
public static void main(String[] args) throws IOException, InterruptedException { System.out.println("WARNING: This will indiscriminately delete every object in a given bucket. This operation cannot be undone."); final String bucketName = prompt("Bucket"); if (!"yes".equals(prompt("Are you sure you want to delete all objects in bucket " + bucketName + "? (type 'yes' to continue)"))) { System.out.println("Aborting..."); return; } final TruncateBucket truncater = new TruncateBucket(bucketName); truncater.truncate(); ObjectListing results; do { results = s3.listObjects(bucketName); final List<KeyVersion> keys = results.getObjectSummaries().stream() .map((k) -> new KeyVersion(k.getKey())) .collect(toList()); final DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keys); s3.deleteObjects(deleteRequest); } while (results.isTruncated()); }
/** * 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); }
/** * {@inheritDoc} */ @Override public void removeValues(final Collection<String> ids) throws AlexaStateException { super.removeValues(ids); final List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(); ids.forEach(id -> keys.addAll(Arrays.asList( new DeleteObjectsRequest.KeyVersion(getUserScopedFilePath(id)), new DeleteObjectsRequest.KeyVersion(getAppScopedFilePath(id))))); final DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName) .withKeys(keys); awsClient.deleteObjects(deleteObjectsRequest); }
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and at least\n" + "one object name (key) to delete.\n" + "\n" + "Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String bucket_name = args[0]; String[] object_keys = Arrays.copyOfRange(args, 1, args.length); System.out.println("Deleting objects from S3 bucket: " + bucket_name); for (String k : object_keys) { System.out.println(" * " + k); } final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name) .withKeys(object_keys); s3.deleteObjects(dor); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
@Override public void deleteDirectory(final S3FileTransferRequestParamsDto params) { LOGGER.info("Deleting keys/key versions from S3... s3KeyPrefix=\"{}\" s3BucketName=\"{}\"", params.getS3KeyPrefix(), params.getS3BucketName()); Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()), "Deleting from root directory is not allowed."); try { // List S3 versions. List<DeleteObjectsRequest.KeyVersion> keyVersions = listVersions(params); LOGGER.info("Found keys/key versions in S3 for deletion. s3KeyCount={} s3KeyPrefix=\"{}\" s3BucketName=\"{}\"", keyVersions.size(), params.getS3KeyPrefix(), params.getS3BucketName()); // In order to avoid a MalformedXML AWS exception, we send delete request only when we have any key versions to delete. if (!keyVersions.isEmpty()) { // Create an S3 client. AmazonS3Client s3Client = getAmazonS3(params); try { // Delete the key versions. deleteKeyVersions(s3Client, params.getS3BucketName(), keyVersions); } finally { s3Client.shutdown(); } } } catch (AmazonClientException e) { throw new IllegalStateException(String .format("Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()), e); } }
@Override public void deleteFileList(final S3FileTransferRequestParamsDto params) { LOGGER.info("Deleting a list of objects from S3... s3BucketName=\"{}\" s3KeyCount={}", params.getS3BucketName(), params.getFiles().size()); try { // In order to avoid a MalformedXML AWS exception, we send delete request only when we have any keys to delete. if (!params.getFiles().isEmpty()) { // Create an S3 client. AmazonS3Client s3Client = getAmazonS3(params); try { // Build a list of keys to be deleted. List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(); for (File file : params.getFiles()) { keys.add(new DeleteObjectsRequest.KeyVersion(file.getPath().replaceAll("\\\\", "/"))); } // Delete the keys. deleteKeyVersions(s3Client, params.getS3BucketName(), keys); } finally { s3Client.shutdown(); } } } catch (Exception e) { throw new IllegalStateException( String.format("Failed to delete a list of keys from bucket \"%s\". Reason: %s", params.getS3BucketName(), e.getMessage()), e); } }
/** * Deletes a list of keys/key versions from the specified S3 bucket. * * @param s3Client the S3 client * @param s3BucketName the S3 bucket name * @param keyVersions the list of S3 keys/key versions */ private void deleteKeyVersions(AmazonS3Client s3Client, String s3BucketName, List<DeleteObjectsRequest.KeyVersion> keyVersions) { // Create a request to delete multiple objects in the specified bucket. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3BucketName); // The Multi-Object Delete request can contain a list of up to 1000 keys. for (int i = 0; i < keyVersions.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++) { List<DeleteObjectsRequest.KeyVersion> keysSubList = keyVersions.subList(i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(keyVersions.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST)); multiObjectDeleteRequest.setKeys(keysSubList); try { s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client); } catch (MultiObjectDeleteException multiObjectDeleteException) { logMultiObjectDeleteException(multiObjectDeleteException); throw multiObjectDeleteException; } LOGGER.info("Successfully requested the deletion of the listed below keys/key versions from the S3 bucket. s3KeyCount={} s3BucketName=\"{}\"", keysSubList.size(), s3BucketName); for (DeleteObjectsRequest.KeyVersion keyVersion : keysSubList) { LOGGER.info("s3Key=\"{}\" s3VersionId=\"{}\"", keyVersion.getKey(), keyVersion.getVersion()); } } }
@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); }
/** * Method delete all <i>objects</i> from <i>bucketName</i> in groups by 1000 * elements * * @param bucketName * @param objects */ public void delete(String bucketName, List<KeyVersion> objects) { ReactiveSeq.fromList(objects) .grouped(1000) .forEach(l -> { DeleteObjectsRequest req = new DeleteObjectsRequest( bucketName); req.setKeys(l.toList()); client.deleteObjects(req); }); }
@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); }
@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; }
private static void deleteObjects(AmazonS3 s3, String s3BucketName, List<S3ObjectSummary> objSummaries, List<DeleteObjectsRequest.KeyVersion> keys, int accum){ Logger.log(new Throwable(), "deleting " + (objSummaries.size() - accum) + " objects..."); DeleteObjectsRequest deleteResquests = new DeleteObjectsRequest(s3BucketName); deleteResquests.setKeys(keys); s3.deleteObjects(deleteResquests); }
@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)); }
@Test public void testDeleteAllKeysOnPartialPath() { AmazonS3 client = mock(AmazonS3.class); S3StoreService service = new S3StoreService(client, S3_BUCKET, ""); String path = "path"; String key = "my-key"; S3ObjectSummary summary = new S3ObjectSummary(); summary.setKey(key); ObjectListing listing = mock(ObjectListing.class); when(listing.getObjectSummaries()).thenReturn(Lists.newArrayList(summary)); when(client.listObjects(S3_BUCKET, path)).thenReturn(listing); // invoke method under test service.deleteAllKeysOnPartialPath(path); ArgumentCaptor<DeleteObjectsRequest> request = ArgumentCaptor.forClass(DeleteObjectsRequest.class); verify(client, times(1)).deleteObjects(request.capture()); assertEquals(key, request.getValue().getKeys().get(0).getKey()); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) { return s3Client.deleteObjects(deleteObjectsRequest); }
@Override public List<DeleteObjectsRequest.KeyVersion> listVersions(final S3FileTransferRequestParamsDto params) { Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()), "Listing of S3 key versions from root directory is not allowed."); AmazonS3Client s3Client = getAmazonS3(params); List<DeleteObjectsRequest.KeyVersion> keyVersions = new ArrayList<>(); try { ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(params.getS3BucketName()).withPrefix(params.getS3KeyPrefix()); VersionListing versionListing; do { versionListing = s3Operations.listVersions(listVersionsRequest, s3Client); for (S3VersionSummary versionSummary : versionListing.getVersionSummaries()) { keyVersions.add(new DeleteObjectsRequest.KeyVersion(versionSummary.getKey(), versionSummary.getVersionId())); } listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker()); listVersionsRequest.setVersionIdMarker(versionListing.getNextVersionIdMarker()); } while (versionListing.isTruncated()); } catch (AmazonS3Exception amazonS3Exception) { if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(amazonS3Exception.getErrorCode())) { throw new IllegalArgumentException("The specified bucket '" + params.getS3BucketName() + "' does not exist.", amazonS3Exception); } throw new IllegalStateException("Error accessing S3", amazonS3Exception); } catch (AmazonClientException e) { throw new IllegalStateException(String .format("Failed to list keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()), e); } finally { // Shutdown the AmazonS3Client instance to release resources. s3Client.shutdown(); } return keyVersions; }
@Test public void testDeleteDirectoryMultiObjectDeleteException() { // Create an S3 file transfer request parameters DTO to access S3 objects. S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto(); s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME); s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX); // Create a retry policy. RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true); // Create an S3 version summary. S3VersionSummary s3VersionSummary = new S3VersionSummary(); s3VersionSummary.setKey(S3_KEY); s3VersionSummary.setVersionId(S3_VERSION_ID); // Create a version listing. VersionListing versionListing = new VersionListing(); versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary)); // Create a delete error. MultiObjectDeleteException.DeleteError deleteError = new MultiObjectDeleteException.DeleteError(); deleteError.setKey(S3_KEY); deleteError.setVersionId(S3_VERSION_ID); deleteError.setCode(ERROR_CODE); deleteError.setMessage(ERROR_MESSAGE); // Create a multi object delete exception. MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(Arrays.asList(deleteError), new ArrayList<>()); // Mock the external calls. when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy); when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing); when(s3Operations.deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class))).thenThrow(multiObjectDeleteException); // Try to call the method under test. try { s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto); } catch (IllegalStateException e) { assertEquals(String.format("Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". " + "Reason: One or more objects could not be deleted (Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null)", S3_KEY_PREFIX, S3_BUCKET_NAME), e.getMessage()); } // Verify the external calls. verify(retryPolicyFactory, times(2)).getRetryPolicy(); verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class)); verify(s3Operations).deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class)); verifyNoMoreInteractionsHelper(); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException { return null; }
private void delete(Collection<String> keyBuffer) { final String[] keyArray = keyBuffer.toArray(new String[keyBuffer.size()]); final DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keyArray); s3.deleteObjects(deleteRequest); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
/** * Lists all S3 versions matching the S3 key prefix in the given bucket (S3 bucket name). The S3 bucket name and S3 key prefix that identify the S3 versions * to get listed are taken from the S3 file transfer request parameters DTO. * * @param params the S3 file transfer request parameters * * @return the list of all S3 versions that match the prefix in the given bucket */ public List<DeleteObjectsRequest.KeyVersion> listVersions(final S3FileTransferRequestParamsDto params);
/** * 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);
public static void deleteBucket(String s3BucketName) throws TalesException{ try{ AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(Config.getAWSAccessKeyId(), Config.getAWSSecretAccessKey())); if(s3.doesBucketExist(s3BucketName)) { // gets all the s3 objects Logger.log(new Throwable(), "getting the list of all the objects saved in the bucket"); ObjectListing current = s3.listObjects(s3BucketName); List<S3ObjectSummary> objSummaries = current.getObjectSummaries(); while (current.isTruncated()) { current = s3.listNextBatchOfObjects(current); objSummaries.addAll(current.getObjectSummaries()); } // deletes the files int i = 0; int accum = 0; List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<DeleteObjectsRequest.KeyVersion>(); for(S3ObjectSummary objSummary: objSummaries){ keys.add(new DeleteObjectsRequest.KeyVersion(objSummary.getKey())); i++; accum++; if(i > 500){ i = 0; deleteObjects(s3, s3BucketName, objSummaries, keys, accum); keys.clear(); } } if(keys.size() > 0){ deleteObjects(s3, s3BucketName, objSummaries, keys, accum); } // deletes the bucket s3.deleteBucket(s3BucketName); }else{ Logger.log(new Throwable(), "bucket not found"); } }catch(Exception e){ throw new TalesException(new Throwable(), e); } }