Aws多个对象删除

时间:2016-03-01 15:05:05

标签: java amazon-web-services amazon-s3

我正在尝试删除多个对象,但它不会删除。我没有得到任何例外。如果我去单删除,它没有问题。

以下代码我正在使用

public void deleteImage(){
        List<KeyVersion> amazonKeys = new ArrayList<KeyVersion>();
        amazonKeys.add(new KeyVersion("compressedc1eac77b-9c38-4036-9770-34a77a163bb0.jpeg"));
        amazonKeys.add(new KeyVersion("compressedb52adf1e-5155-48b6-9051-bb679601f5ee.jpeg"));
        imageService.removeS3Files("mubucketname/dev/3123",amazonKeys);
    }

我的服务是

public void removeS3Files(String bucketName,List<KeyVersion> keys){

        log.debug("deleting multiple objects from s3 with bucket::" + bucketName);
        try{
            DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName);
            multiObjectDeleteRequest.setKeys(keys);
            AmazonS3 s3client = new AmazonS3Client(CustomAwsCredentials.getInstance(envConfiguration));
            s3client.setEndpoint(Constant.AWS_ENDPOINT);
            DeleteObjectsResult deleteObjectsResult = s3client.deleteObjects(multiObjectDeleteRequest);
            System.out.println(deleteObjectsResult.getDeletedObjects());
        }catch(AmazonServiceException exception){
            log.debug("Caught an AmazonServiceException.");
            log.debug("Error Message:    " + exception.getMessage());
        }catch (AmazonClientException clientException) {
            log.debug("Caught an AmazonClientException.");
            log.debug("Error Message:    " + clientException.getMessage());
        }
    }

我在数据库中的数据存储看起来像

bucketname/dev/3123/compressedc1eac77b-9c38-4036-9770-34a77a163bb0.jpeg
bucketname/dev/3123/compressedb52adf1e-5155-48b6-9051-bb679601f5ee.jpeg

我使用下面的代码删除单个对象(工作正常)

try{
            AmazonS3 s3client = new AmazonS3Client(CustomAwsCredentials.getInstance(envConfiguration));
            System.out.println(s3client.doesBucketExist(bucketName));
            s3client.setEndpoint(Constant.AWS_ENDPOINT);
            s3client.deleteObject(bucketName, key);
        }catch(AmazonServiceException exception){
            log.debug("Caught an AmazonServiceException.");
            log.debug("Error Message:    " + exception.getMessage());
        }catch (AmazonClientException clientException) {
            log.debug("Caught an AmazonClientException.");
            log.debug("Error Message:    " + clientException.getMessage());
        }

请帮我解决多个对象删除中缺少的问题?

提前致谢

1 个答案:

答案 0 :(得分:1)

这不是有效的存储桶名称:

mubucketname/dev/3123

存储桶名称与密钥分开,您无法从存储桶名称的密钥中添加路径前缀。试试这个:

List<KeyVersion> keys = new ArrayList<KeyVersion>();
keys.add(new KeyVersion("dev/3123/compressedc1eac77b-9c38-4036-9770-34a77a163bb0.jpeg"));
keys.add(new KeyVersion("dev/3123/compressedb52adf1e-5155-48b6-9051-bb679601f5ee.jpeg"));
DeleteObjectsRequest request = new DeleteObjectsRequest("mubucketname").withKeys(keys);
DeleteObjectsResult result = s3client.deleteObjects(request);