JasminDan
JasminDan

Reputation: 643

aws s3 delete object not working

I'm trying to upload/delete image to/from aws s3 bucket using spring boot.

public class AmazonClient {
    private AmazonS3 s3client;

private void initializeAmazon() {
    AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
    this.s3client = AmazonS3ClientBuilder.standard().withRegion(region).withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
}
private void uploadFileTos3bucket(String fileName, File file) {
    s3client.putObject(new PutObjectRequest(bucketName, fileName, file)
            .withCannedAcl(CannedAccessControlList.PublicRead));
}

public void deleteFileFromS3Bucket(String fileUrl) {
    String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
    s3client.deleteObject(new DeleteObjectRequest(bucketName + "/", fileName));
}
}

The upload function works well, I can see the file has been uploaded to the s3 bucket, but the delete function seems malfunctioning, I get a successful message but the file is still in the bucket.

Thanks in advance if anyone could help me to figure out the problem.

Upvotes: 8

Views: 15850

Answers (4)

Sofia Chand
Sofia Chand

Reputation: 1

The only thing that worked for me is deleting it through Cyberduck (I neither work for nor am promoting Cyberduck, I genuinely used it and it worked). Here are the steps of what I did:

  1. Download and install Cyberduck.

  2. Click on Open Connection

  3. Select Amazon S3 from the dropdown (default would be FTP)

  4. Enter your access key ID and secret Access key (if you don't have one then you need to create one on your s3 bucket through IAM on AWS).

  5. You will see a list your S3 buckets. Select the file or folder or bucket you want to delete, right click and delete. Even files with 0kb show up here and can be deleted.

Upvotes: -3

Gennadii Ianchev
Gennadii Ianchev

Reputation: 131

public boolean deleteFileFromS3Bucket(String fileUrl) {
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
        try {
            DeleteObjectsRequest delObjReq = new DeleteObjectsRequest(bucketName).withKeys(fileName);
            s3client.deleteObjects(delObjReq);
            return true;
        } catch (SdkClientException s) {
            return false;
        }
    }

For me, working here is an option.

Upvotes: 2

JasminDan
JasminDan

Reputation: 643

Just found out that I added an additional slash in new DeleteObjectRequest.

Upvotes: 4

Thiyagu
Thiyagu

Reputation: 17920

From the javadoc of deleteObject (emphasis mine)

Deletes the specified object in the specified bucket. Once deleted, the object can only be restored if versioning was enabled when the object was deleted.

If attempting to delete an object that does not exist, Amazon S3 will return a success message instead of an error message.

So, most probably the path (fileName) you construct in deleteFileFromS3Bucket does not point to an S3 object.

EDIT

I'm updating my answer based on the comments:

The file name used has special characters (: in the provided example) which gets URL encoded (percent encoded). This encoded URL cannot be used to retrieve or delete the S3 object as the percent in the URL would get encoded again(% gets encoded to %25).

The encoded URL has to be decoded. One way is to use java.net.URLDecoder

URLDecoder.decode(encodedPath, "UTF-8")

Upvotes: 3

Related Questions