Example usage for com.amazonaws.services.s3.model DeleteObjectsResult getDeletedObjects

List of usage examples for com.amazonaws.services.s3.model DeleteObjectsResult getDeletedObjects

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model DeleteObjectsResult getDeletedObjects.

Prototype

public List<DeletedObject> getDeletedObjects() 

Source Link

Document

Returns the list of successfully deleted objects from this request.

Usage

From source file:io.milton.s3.AmazonS3ManagerImpl.java

License:Open Source License

@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;
    }/*w  ww.  j  av  a2  s  .c  o m*/

    // 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;
}

From source file:org.alanwilliamson.amazon.s3.Delete.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    AmazonKey amazonKey = getAmazonKey(_session, argStruct);
    AmazonS3 s3Client = getAmazonS3(amazonKey);

    String bucket = getNamedStringParam(argStruct, "bucket", null);
    if (bucket == null)
        throwException(_session, "Please specify a bucket");

    cfData key = getNamedParam(argStruct, "key");

    try {//  w  w  w  .j  av a  2s. c o  m

        if (key.getDataType() == cfData.CFARRAYDATA) {
            DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucket);

            List keysT = new ArrayList();

            cfArrayData arrData = (cfArrayData) key;

            for (int x = 0; x < arrData.size(); x++) {

                String k = arrData.getData(x + 1).toString();
                if (k.charAt(0) == '/')
                    k = k.substring(1);

                keysT.add(new KeyVersion(k));
            }

            multiObjectDeleteRequest.setKeys(keysT);
            DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);

            return new cfNumberData(delObjRes.getDeletedObjects().size());

        } else {

            String k = key.toString();
            if (k.charAt(0) == '/')
                k = k.substring(1);

            s3Client.deleteObject(new DeleteObjectRequest(bucket, k));
            return new cfNumberData(1);
        }

    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
        return new cfNumberData(0);
    }
}

From source file:org.apache.jackrabbit.aws.ext.ds.S3Backend.java

License:Apache License

@Override
public Set<DataIdentifier> deleteAllOlderThan(long min) throws DataStoreException {
    long start = System.currentTimeMillis();
    // S3 stores lastModified to lower boundary of timestamp in ms.
    // and hence min is reduced by 1000ms.
    min = min - 1000;//from  www  .ja  v a2 s.  c o m
    Set<DataIdentifier> deleteIdSet = new HashSet<DataIdentifier>(30);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectListing prevObjectListing = s3service.listObjects(bucket);
        while (true) {
            List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                DataIdentifier identifier = new DataIdentifier(getIdentifierName(s3ObjSumm.getKey()));
                long lastModified = s3ObjSumm.getLastModified().getTime();
                LOG.debug("Identifier [{}]'s lastModified = [{}]", identifier, lastModified);
                if (!store.isInUse(identifier) && lastModified < min) {
                    LOG.debug("add id [{}] to delete lists", s3ObjSumm.getKey());
                    deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
                    deleteIdSet.add(identifier);
                }
            }
            if (deleteList.size() > 0) {
                DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
                delObjsReq.setKeys(deleteList);
                DeleteObjectsResult dobjs = s3service.deleteObjects(delObjsReq);
                if (dobjs.getDeletedObjects().size() != deleteList.size()) {
                    throw new DataStoreException(
                            "Incomplete delete object request. only  " + dobjs.getDeletedObjects().size()
                                    + " out of " + deleteList.size() + " are deleted");
                } else {
                    LOG.debug("[{}] records deleted from datastore", deleteList);
                }
            }
            if (!prevObjectListing.isTruncated()) {
                break;
            }
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.info(
            "deleteAllOlderThan: min=[{}] exit. Deleted[{}] records. Number of records deleted [{}] took [{}]ms",
            new Object[] { min, deleteIdSet, deleteIdSet.size(), (System.currentTimeMillis() - start) });
    return deleteIdSet;
}

From source file:org.apache.jackrabbit.aws.ext.ds.S3Backend.java

License:Apache License

/**
 * This method rename object keys in S3 concurrently. The number of
 * concurrent threads is defined by 'maxConnections' property in
 * aws.properties. As S3 doesn't have "move" command, this method simulate
 * move as copy object object to new key and then delete older key.
 *///from ww w  .  j  ava 2s .  c  o  m
private void renameKeys() throws DataStoreException {
    long startTime = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    long count = 0;
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectListing prevObjectListing = s3service.listObjects(bucket, KEY_PREFIX);
        List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
        int nThreads = Integer.parseInt(properties.getProperty("maxConnections"));
        ExecutorService executor = Executors.newFixedThreadPool(nThreads,
                new NamedThreadFactory("s3-object-rename-worker"));
        boolean taskAdded = false;
        while (true) {
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                executor.execute(new KeyRenameThread(s3ObjSumm.getKey()));
                taskAdded = true;
                count++;
                deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
            }
            if (!prevObjectListing.isTruncated())
                break;
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        executor.shutdown();

        try {
            // Wait until all threads are finish
            while (taskAdded && !executor.awaitTermination(10, TimeUnit.SECONDS)) {
                LOG.info("Rename S3 keys tasks timedout. Waiting again");
            }
        } catch (InterruptedException ie) {

        }
        LOG.info("Renamed [{}] keys, time taken [{}]sec", count,
                ((System.currentTimeMillis() - startTime) / 1000));
        // Delete older keys.
        if (deleteList.size() > 0) {
            DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
            int batchSize = 500, startIndex = 0, size = deleteList.size();
            int endIndex = batchSize < size ? batchSize : size;
            while (endIndex <= size) {
                delObjsReq.setKeys(Collections.unmodifiableList(deleteList.subList(startIndex, endIndex)));
                DeleteObjectsResult dobjs = s3service.deleteObjects(delObjsReq);
                LOG.info("Records[{}] deleted in datastore from index [{}] to [{}]",
                        new Object[] { dobjs.getDeletedObjects().size(), startIndex, (endIndex - 1) });
                if (endIndex == size) {
                    break;
                } else {
                    startIndex = endIndex;
                    endIndex = (startIndex + batchSize) < size ? (startIndex + batchSize) : size;
                }
            }
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.S3Backend.java

License:Apache License

@Override
public Set<DataIdentifier> deleteAllOlderThan(long min) throws DataStoreException {
    long start = System.currentTimeMillis();
    // S3 stores lastModified to lower boundary of timestamp in ms.
    // and hence min is reduced by 1000ms.
    min = min - 1000;/*from ww  w .j a v  a 2s .  c om*/
    Set<DataIdentifier> deleteIdSet = new HashSet<DataIdentifier>(30);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectListing prevObjectListing = s3service.listObjects(bucket);
        while (true) {
            List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                if (!s3ObjSumm.getKey().startsWith(META_KEY_PREFIX)) {
                    DataIdentifier identifier = new DataIdentifier(getIdentifierName(s3ObjSumm.getKey()));
                    long lastModified = s3ObjSumm.getLastModified().getTime();
                    LOG.debug("Identifier [{}]'s lastModified = [{}]", identifier, lastModified);
                    if (lastModified < min && store.confirmDelete(identifier)
                    // confirm once more that record's lastModified < min
                    //  order is important here
                            && s3service.getObjectMetadata(bucket, s3ObjSumm.getKey()).getLastModified()
                                    .getTime() < min) {

                        store.deleteFromCache(identifier);
                        LOG.debug("add id [{}] to delete lists", s3ObjSumm.getKey());
                        deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
                        deleteIdSet.add(identifier);
                    }
                }
            }
            if (deleteList.size() > 0) {
                DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
                delObjsReq.setKeys(deleteList);
                DeleteObjectsResult dobjs = s3service.deleteObjects(delObjsReq);
                if (dobjs.getDeletedObjects().size() != deleteList.size()) {
                    throw new DataStoreException(
                            "Incomplete delete object request. only  " + dobjs.getDeletedObjects().size()
                                    + " out of " + deleteList.size() + " are deleted");
                } else {
                    LOG.debug("[{}] records deleted from datastore", deleteList);
                }
            }
            if (!prevObjectListing.isTruncated()) {
                break;
            }
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.info(
            "deleteAllOlderThan: min=[{}] exit. Deleted[{}] records. Number of records deleted [{}] took [{}]ms",
            new Object[] { min, deleteIdSet, deleteIdSet.size(), (System.currentTimeMillis() - start) });
    return deleteIdSet;
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.S3Backend.java

License:Apache License

/**
 * This method rename object keys in S3 concurrently. The number of
 * concurrent threads is defined by 'maxConnections' property in
 * aws.properties. As S3 doesn't have "move" command, this method simulate
 * move as copy object object to new key and then delete older key.
 *///from   ww w . j ava 2 s.c  o m
private void renameKeys() throws DataStoreException {
    long startTime = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    long count = 0;
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectListing prevObjectListing = s3service.listObjects(bucket);
        List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
        int nThreads = Integer.parseInt(properties.getProperty("maxConnections"));
        ExecutorService executor = Executors.newFixedThreadPool(nThreads,
                new NamedThreadFactory("s3-object-rename-worker"));
        boolean taskAdded = false;
        while (true) {
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                executor.execute(new KeyRenameThread(s3ObjSumm.getKey()));
                taskAdded = true;
                count++;
                // delete the object if it follows old key name format
                if (s3ObjSumm.getKey().startsWith(KEY_PREFIX)) {
                    deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
                }
            }
            if (!prevObjectListing.isTruncated())
                break;
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        executor.shutdown();

        try {
            // Wait until all threads are finish
            while (taskAdded && !executor.awaitTermination(10, TimeUnit.SECONDS)) {
                LOG.info("Rename S3 keys tasks timedout. Waiting again");
            }
        } catch (InterruptedException ie) {

        }
        LOG.info("Renamed [{}] keys, time taken [{}]sec", count,
                ((System.currentTimeMillis() - startTime) / 1000));
        // Delete older keys.
        if (deleteList.size() > 0) {
            DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
            int batchSize = 500, startIndex = 0, size = deleteList.size();
            int endIndex = batchSize < size ? batchSize : size;
            while (endIndex <= size) {
                delObjsReq.setKeys(Collections.unmodifiableList(deleteList.subList(startIndex, endIndex)));
                DeleteObjectsResult dobjs = s3service.deleteObjects(delObjsReq);
                LOG.info("Records[{}] deleted in datastore from index [{}] to [{}]",
                        new Object[] { dobjs.getDeletedObjects().size(), startIndex, (endIndex - 1) });
                if (endIndex == size) {
                    break;
                } else {
                    startIndex = endIndex;
                    endIndex = (startIndex + batchSize) < size ? (startIndex + batchSize) : size;
                }
            }
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

public void deleteMultipleObjects(List<String> listOfKeysToRemove) {
    if (listOfKeysToRemove == null || listOfKeysToRemove.isEmpty()) {
        return;//from   w  ww  . j a  v a  2s. c  om
    }

    S3Configuration s3config = s3ConfigurationService.lookupS3Configuration();
    AmazonS3Client s3Client = getAmazonS3Client(s3config);
    String bucketName = s3config.getDefaultBucketName();

    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName);

    List<KeyVersion> keys = new ArrayList<KeyVersion>();

    for (String targetKey : listOfKeysToRemove) {
        keys.add(new KeyVersion(targetKey));
    }

    multiObjectDeleteRequest.setKeys(keys);

    try {
        DeleteObjectsResult delObjResult = s3Client.deleteObjects(multiObjectDeleteRequest);
        if (LOG.isTraceEnabled()) {
            String s = listOfKeysToRemove.stream().collect(Collectors.joining(",\n\t"));

            LOG.trace(String.format("Successfully deleted %d items:\n\t%s",
                    delObjResult.getDeletedObjects().size(), s));
        }
    } catch (MultiObjectDeleteException e) {
        if (LOG.isTraceEnabled()) {
            LOG.trace(String.format("%s \n", e.getMessage()));
            LOG.trace(
                    String.format("No. of objects successfully deleted = %s\n", e.getDeletedObjects().size()));
            LOG.trace(String.format("No. of objects failed to delete = %s\n", e.getErrors().size()));
            LOG.trace(String.format("Printing error data...\n"));
            for (DeleteError deleteError : e.getErrors()) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(String.format("Object Key: %s\t%s\t%s\n", deleteError.getKey(),
                            deleteError.getCode(), deleteError.getMessage()));
                }
            }
        }
        throw new RuntimeException("No. of objects failed to delete = " + e.getErrors().size(), e);
    }
}