I have list of java objects which I need to upload to [Amazon] s3. Currently I am uplaoding each object one by one which is quite inefficient. Each object in the list is first serialized to JSON and separated by a newline, i.e. \n
private ByteBufferOutputStream serialize(final Object object) {
try {
final ByteBufferOutputStream outputStream = new ByteBufferOutputStream(128, true, false);
Json.writeValueAsToOutputStream(object, outputStream);
outputStream.write('\n');
return outputStream;
} catch (final Exception e) {
log.error("Error serializing data {}", object, e);
return null;
}
}
for (Data event : events) {
final ByteBufferOutputStream serializedEvent = serialize(event);
if (serializedEvent == null) {
continue;
}
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
try {
s3Client.putObject(objectRequest, RequestBody.fromByteBuffer(serializedEvent.toByteBuffer()));
} catch (S3Exception e) {
log.error("Failed to send raw data events to S3", e);
}
}
How can I upload all the objects in a list only once after serialization ?