简体   繁体   中英

Not able to Delete Objects from AWS S3 using java?

I am trying to list Objects based on prefix folder and delete each object.

AWSCredentials awsCredentials = new BasicAWSCredentials("accesskey", "secretkey");
AmazonS3 s3client = new AmazonS3Client(awsCredentials);
ListObjectsRequest listObjectsRequest = 
               new ListObjectsRequest("bucketname","folder",null,"/",1000);

s3client
      .listObjects(listObjectsRequest)
      .getObjectSummaries()
      .forEach(
          (file) -> {
            DeleteObjectRequest deleteRequest = new DeleteObjectRequest("bucketname", file.getKey());
            s3client.deleteObject(deleteRequest);
          });

But i am getting following Exception

com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied;

    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4187)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4134)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4128)
    at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:839)

The keys I am using are valid. I can perform other operations like getobject() .

What am I doing wrong?

Question title looks wrong. You are able to list objects; but not delete objects. This looks to be permission problem.

User associated with the keys does not have permission to delete objects on that S3 bucket. Easiest way to check this would be to login as that user on AWS console ( if that user has aws console username/password); and try deleting the object manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM