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.
You could do something like this:
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(); }
Convert output stream to input stream via byte array:
InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
Pass this stream to S3 PutObjectRequest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With