List of usage examples for com.amazonaws.services.s3.model DeleteObjectsRequest setKeys
public void setKeys(List<KeyVersion> keys)
From source file:org.finra.dm.dao.impl.S3DaoImpl.java
License:Apache License
@Override public void deleteFileList(final S3FileTransferRequestParamsDto params) { AmazonS3Client s3Client = null;//from ww w . j ava 2 s . c o m LOGGER.info(String.format("Deleting %d keys/objects from s3://%s ...", params.getFiles().size(), params.getS3BucketName())); 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()) { // 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("\\\\", "/"))); } DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(params.getS3BucketName()); s3Client = getAmazonS3(params); // The Multi-Object Delete request can contain a list of up to 1000 keys. for (int i = 0; i < keys.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++) { List<DeleteObjectsRequest.KeyVersion> keysSubList = keys.subList( i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(keys.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST)); multiObjectDeleteRequest.setKeys(keysSubList); s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client); LOGGER.info(String.format( "Successfully requested the deletion of the following %d keys/objects from bucket \"%s\":", keysSubList.size(), params.getS3BucketName())); for (DeleteObjectsRequest.KeyVersion keyVersion : keysSubList) { LOGGER.info(String.format(" s3://%s/%s", params.getS3BucketName(), keyVersion.getKey())); } } } } catch (Exception e) { throw new IllegalStateException( String.format("Failed to delete a list of keys/objects from bucket \"%s\". Reason: %s", params.getS3BucketName(), e.getMessage()), e); } finally { // Shutdown the AmazonS3Client instance to release resources. if (s3Client != null) { s3Client.shutdown(); } } }
From source file:org.finra.dm.dao.impl.S3DaoImpl.java
License:Apache License
@Override public void deleteDirectory(final S3FileTransferRequestParamsDto params) { AmazonS3Client s3Client = null;//w w w . ja v a 2s .c o m LOGGER.info(String.format("Deleting keys/objects from s3://%s/%s ...", params.getS3BucketName(), params.getS3KeyPrefix())); Assert.hasText(params.getS3KeyPrefix(), "Deleting from root directory is not allowed."); try { // List S3 object including any 0 byte objects that represent S3 directories. List<StorageFile> storageFiles = listObjectsMatchingKeyPrefix(params, false); LOGGER.info(String.format("Found %d keys/objects in s3://%s/%s ...", storageFiles.size(), params.getS3BucketName(), params.getS3KeyPrefix())); // In order to avoid a MalformedXML AWS exception, we send delete request only when we have any keys to delete. if (!storageFiles.isEmpty()) { DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(params.getS3BucketName()); s3Client = getAmazonS3(params); // The Multi-Object Delete request can contain a list of up to 1000 keys. for (int i = 0; i < storageFiles.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++) { // Prepare a list of S3 object keys to be deleted. List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(); for (StorageFile storageFile : storageFiles.subList(i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(storageFiles.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST))) { keys.add(new DeleteObjectsRequest.KeyVersion(storageFile.getFilePath())); } // Delete the S3 objects. multiObjectDeleteRequest.setKeys(keys); s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client); LOGGER.info(String.format( "Successfully deleted the following %d keys/objects with prefix \"%s\" from bucket \"%s\":", keys.size(), params.getS3KeyPrefix(), params.getS3BucketName())); for (DeleteObjectsRequest.KeyVersion keyVersion : keys) { LOGGER.info(String.format(" s3://%s/%s", params.getS3BucketName(), keyVersion.getKey())); } } } } catch (AmazonClientException e) { throw new IllegalStateException( String.format("Failed to delete keys/objects with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()), e); } finally { // Shutdown the AmazonS3Client instance to release resources. if (s3Client != null) { s3Client.shutdown(); } } }
From source file:org.finra.herd.dao.impl.S3DaoImpl.java
License:Apache License
/** * Deletes a list of keys/key versions from the specified S3 bucket. * * @param s3Client the S3 client/*from ww w . j ava 2s.c om*/ * @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()); } } }
From source file:org.geowebcache.s3.S3BlobStore.java
License:Open Source License
@Override public boolean delete(final TileRange tileRange) throws StorageException { final String coordsPrefix = keyBuilder.coordinatesPrefix(tileRange); if (!s3Ops.prefixExists(coordsPrefix)) { return false; }/*from ww w.ja v a 2 s. c om*/ final Iterator<long[]> tileLocations = new AbstractIterator<long[]>() { // TileRange iterator with 1x1 meta tiling factor private TileRangeIterator trIter = new TileRangeIterator(tileRange, new int[] { 1, 1 }); @Override protected long[] computeNext() { long[] gridLoc = trIter.nextMetaGridLocation(new long[3]); return gridLoc == null ? endOfData() : gridLoc; } }; if (listeners.isEmpty()) { // if there are no listeners, don't bother requesting every tile // metadata to notify the listeners Iterator<List<long[]>> partition = Iterators.partition(tileLocations, 1000); final TileToKey tileToKey = new TileToKey(coordsPrefix, tileRange.getMimeType()); while (partition.hasNext() && !shutDown) { List<long[]> locations = partition.next(); List<KeyVersion> keys = Lists.transform(locations, tileToKey); DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName); req.setQuiet(true); req.setKeys(keys); conn.deleteObjects(req); } } else { long[] xyz; String layerName = tileRange.getLayerName(); String gridSetId = tileRange.getGridSetId(); String format = tileRange.getMimeType().getFormat(); Map<String, String> parameters = tileRange.getParameters(); while (tileLocations.hasNext()) { xyz = tileLocations.next(); TileObject tile = TileObject.createQueryTileObject(layerName, xyz, gridSetId, format, parameters); tile.setParametersId(tileRange.getParametersId()); delete(tile); } } return true; }
From source file:org.geowebcache.s3.TemporaryS3Folder.java
License:Open Source License
public void delete() { checkState(isConfigured(), "client not configured."); if (temporaryPrefix == null) { return;//from w ww .j av a 2s .c o m } Iterable<S3ObjectSummary> objects = S3Objects.withPrefix(s3, bucket, temporaryPrefix); Iterable<List<S3ObjectSummary>> partition = Iterables.partition(objects, 1000); for (List<S3ObjectSummary> os : partition) { List<KeyVersion> keys = Lists.transform(os, new Function<S3ObjectSummary, KeyVersion>() { @Override public KeyVersion apply(S3ObjectSummary input) { KeyVersion k = new KeyVersion(input.getKey()); return k; } }); DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucket); deleteRequest.setKeys(keys); s3.deleteObjects(deleteRequest); } }
From source file:org.mule.module.s3.simpleapi.SimpleAmazonS3AmazonDevKitImpl.java
License:Open Source License
public void deleteObjects(@NotNull String bucketName, @NotNull List<KeyVersion> keys) { Validate.notNull(bucketName);//ww w . j a v a 2 s .com Validate.notNull(keys); Validate.notEmpty(keys); DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName); List<DeleteObjectsRequest.KeyVersion> deleteKeysRequest = new ArrayList<DeleteObjectsRequest.KeyVersion>(); for (KeyVersion key : keys) { deleteKeysRequest.add(new DeleteObjectsRequest.KeyVersion(key.getValue(), key.getVersion())); } deleteObjectsRequest.setKeys(deleteKeysRequest); s3.deleteObjects(deleteObjectsRequest); }