根据模式匹配从S3删除文件?

时间:2013-07-09 05:37:28

标签: amazon-web-services amazon-s3 amazon delete-file

如果S3中的API可用于根据模式匹配从S3存储桶中删除密钥,是否有人可以指出?

我能做的一种方法是:

  1. 列出存储桶中的所有密钥
  2. 通过java regex匹配它们
  3. 然后获取所需的结果集。
  4. 是否有内置API?

1 个答案:

答案 0 :(得分:3)

根据我的解决方案,我只是发布代码,以便它可能对寻找相同场景的人有用:

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