Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

amazon s3 - What is the best way to implement file upload using spring boot in AWS S3 bucket?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...