Search code examples
springspring-bootamazon-s3spring-cloudspring-cloud-aws

How to delete file from S3 using Spring cloud AWS?


I could not find any API or documentation in Spring AWS Cloud to delete an object from S3 bucket. Can someone please let me know how to do it?

The documentation just talks about reading the content using ResourceLoader.

Only option right now I see is to explicitly inject AmazonS3 and call deleteObject.


Solution

  • Spring's Resource API does not support the full lifecycle of operations. The two main interfaces are Resource and WritableResource. There is no API for deletion.

    As an alternative you could use Spring Content for S3 instead. Spring Content Stores are all generic ResourceLoaders that return Resources that DO support deletion. That, by the way, works exactly as you are suggesting.

    Take a look at the reference guide. The base Store interface is the one that should be a straight drop-in for the one from Spring Cloud AWS.

    Simply cast the return from getResource to DeletableResource and call it's delete to delete the object in S3.

    Let me know if this is not clear and I can update with more details.