-3

Working on Java Spring boot application(Web Service using web starter), I have requirement to store images in S3 bucket, Can someone suggest me how to implement?

Hosting application in EC2 instance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

you can use spring-cloud-aws with a s3 capable ResourceLoader:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
   xsi:schemaLocation="http://www.springframework.org/schema/cloud/aws/context http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd">
    <aws-context:context-credentials>
       <!-- ... -->
     </aws-context:context-credentials>
    <aws-context:context-resource-loader/>
</beans>

And inject it into your bean:

public class SimpleResourceLoadingBean {

@Autowired
private ResourceLoader resourceLoader;

public void writeResource() throws IOException {
        Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
        WritableResource writableResource = (WritableResource) resource;
        try (OutputStream outputStream = writableResource.getOutputStream()) {
            outputStream.write("test".getBytes());
        }
    }
}

Here you can find the documentation: http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.2.1.RELEASE/#_uploading_files

Hardi
  • 56
  • 4
  • 1
    Just be aware that this will not work with S3 Server Side Encryption (SSE) because it's not possible to modify the metadata for the upload. Thus, this solution is restrictive. – Rori Stumpf Mar 14 '18 at 20:27
0

Finally I got solution Refer below blog to implement Spring boot AWS s3 upload Image and delete image.

Link Spring boot s3 upload