amazon-web-services - 基于模式匹配从 S3 中删除文件?

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

谁能指出 S3 中是否有 API 可以根据模式匹配从 S3 存储桶中删除 key ?

我能做的一种方法是:

  1. 列出桶中的所有键
  2. 通过java正则匹配它们
  3. 然后获取需要的resultset。

是否有用于此的任何内置 API?

最佳答案

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

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

关于amazon-web-services - 基于模式匹配从 S3 中删除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17540719/

相关文章:

node.js - AWS Lambda 不通过 nodemailer 触发电子邮件,但在本地开发环境中触发

reactjs - 使用 Amplify 实现的登录表单未应用样式

amazon-web-services - 无法使用 UserData bash 脚本标记 EBS 卷

.net - 如何使用 .NET HttpClient 上传到 Amazon S3 而不使用他们的 SDK

http - Amazon S3 CORS header 仅在 OPTIONS(预检)期间显示,而不在 GET 请求期间显示

pdf - 裁剪基于文本的多页 PDF 文档的白边并将其转换为基于图像的 PDF 文档

hadoop - 超过10个节点的大型群集的旋转

amazon-web-services - 如何通过 API Gateway 调用 AWS Step Functions?

python - Amazon S3 权限错误 403,简单上传文本文件

deployment - 删除了自定义 Amazon Beanstalk AMI ID