Home > Enterprise >  How to store data from Spring Boot model to S3 as CVS file - Java
How to store data from Spring Boot model to S3 as CVS file - Java

Time:10-28

I have a model in Spring Boot that looks like this:

public class TransactionHistory {
    public final String id;
    public final String firstName;
    public final String lastName;

    public TransactionHistory(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

I would like to post this data to S3 as CSV file that can be downloaded from S3.

What is the best way to do it?

If it's possible I would like to create CSV in Java code and from code post it to S3, so I don't need to store it locally on computer or in database.

Is this possible?

CodePudding user response:

You can use database to store data and then adding that data to csv file. First create a model class and then declare dependency in pom.xml file

<dependency>
    <groupId>net.sf.supercsv</groupId>
    <artifactId>super-csv</artifactId>
    <version>2.4.0</version>
</dependency>

code the service class and then write a code to export data to csv in controller class. I am attaching a link which contains a complete example of convert data to csv file. That can be easily downloaded and uploaded.

Here is the link: https://www.codejava.net/frameworks/spring-boot/csv-export-example

CodePudding user response:

You may putObject to upload or getObject to download a file. The RequestBody can be created directly from a stream..

Another option would be creating a temp file and using the putObject to upload the file.

Proper use of aws sdk (authentication and authorization) is out of scope of this answer and I assume you will read the documentation.

Regardless that note the S3 is much slower than a local/network disk or databases, so it is definitely not any replacement.

  • Related