Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 528
Laksh.S Avatar asked Nov 07 '22 15:11

Laksh.S


1 Answers

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

like image 193
Ankur Avatar answered Nov 12 '22 23:11

Ankur