Deleting files from S3 based on pattern matching?

Can anyone point if API is available in S3 to delete keys from S3 bucket based on pattern matching ?

The one way I can do is :

  1. List all the keys in the bucket
  2. Match them through java regex
  3. Then obtain resultset needed.

Is there any builtin API for this ?

Upvotes: 3

Views: 3171

Answers (1)

Based on my solution, I am just posting the code so that it might be useful for some one looking for same scenario :

/**
     * Delete keys/objects from buckets with matching prefix 
     * @param bucket
     *        Bucket in which delete operation is performed
     * @param prefix
     *        String to match the pattern on keys.
     * @return
     */
    @Override
    public void deleteFilesInS3(String bucket, String prefix) throws IOException {
        try{
        List<KeyVersion> keys = listAllKeysWithPrefix(bucket, prefix);
        DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucket);
        multiObjectDeleteRequest.setKeys(keys);
        s3EncryptionClient.deleteObjects(multiObjectDeleteRequest);
        }catch(MultiObjectDeleteException e){
            throw new RuntimeException(String.format("Failed to delete files with prefix : %s from bucket : %s ",prefix,bucket),e);
        }catch(AmazonServiceException ase){
            throw new AmazonServiceException(String.format("Failed to delete files with prefix : %s from bucket : %s ",prefix,bucket),ase);
        }catch(AmazonClientException ace){
            throw new AmazonClientException(String.format("Failed to delete files with prefix : %s from bucket : %s ",prefix,bucket),ace);
        }
    }

    /**
     * Lists all the keys matching the prefix in the given bucket.
     * @param bucket
     *        Bucket to search keys
     * @param prefix
     *        String to match the pattern on keys.
     * @return
     */
    @Override
    public List<KeyVersion> listAllKeysWithPrefix(String bucket,String prefix){
        List<KeyVersion> keys = new ArrayList<KeyVersion>();
        try{
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(prefix);
            ObjectListing objectListing = null;
            do{
                objectListing = s3EncryptionClient.listObjects(listObjectsRequest);
                for(S3ObjectSummary objectSummary : objectListing.getObjectSummaries()){
                    keys.add(new KeyVersion(objectSummary.getKey()));
                }
                listObjectsRequest.setMarker(objectListing.getNextMarker());
            }while(objectListing.isTruncated());
        }catch(AmazonServiceException ase){
            throw new AmazonServiceException(String.format("Failed to list files with prefix : %s from bucket : %s ",prefix,bucket),ase);
        }catch(AmazonClientException ace){
            throw new AmazonClientException(String.format("Failed to delete files with prefix : %s from bucket : %s ",prefix,bucket),ace);
        }catch(Exception e){
            throw new RuntimeException(String.format("Failed to delete files with prefix : %s from bucket : %s ",prefix,bucket),e);
        }
        return keys;
    }

Upvotes: 3

Related Questions