Can we write csv file to S3 without creating a file on local in spring boot?

Laksh.S :

I've some set of objects and want to store these data as CSV file on AWS S3 bucket without creating any local file. Can anyone please suggest how could it be smoothly done without impacting performance?

For Eg.: Set<Address> addresses; //Address class contains some fields as city, state, zipcode and country.

It should create a csv file with following headers and data:

City, State, ZipCode, Country
-----------------------------

Mumbai, Maharashtra, 4200091, India

One thing I know is we can write data as InputStream and then pass it to -PutObjectRequest. But InputStream also takes filepath I don't want to waste time in creating temp files and I've multiple operations to do.

PutObjectRequest putObj = new PutObjectRequest(bucketName, KeyName, inputStream, metadata); 
s3client.putObject(putObj);

Thanks in advance for your help and time.

Ankur :

You could do something like this:

  1. Create csv output stream:(Refer:)

Dependency to be added (Refer to above link for the example used):

<dependency>
    <groupId>net.sourceforge.javacsv</groupId>
    <artifactId>javacsv</artifactId>
    <version>2.0</version> </dependency>

Code :

public void createCsv() throws Exception {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    CsvWriter writer = new CsvWriter(stream, ',', Charset
            .forName("ISO-8859-1"));

    writer.setRecordDelimiter(';');
    // WRITE Your CSV Here
    //writer.write("a;b");
    writer.endRecord();
    writer.close();

    stream.close();    }
  1. Convert output stream to input stream via byte array:

    InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());

  2. Pass this stream to S3 PutObjectRequest

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162079&siteId=1