I'd want to store my files as small as possible. So I've decided to make my json file gzipped. I'm currently using my method that create gzip file, upload mongodb and delete file in folder but I've noticed that gridfs has feature which saves inputstream to database.
My questions
If all of my questions answer is possitive. I'd want to know how to use GridFS Inputstream and convert my gzip code to gridfs.
Current Code
try {
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream("pdiskio.dsv"));
OutputStreamWriter osw = new OutputStreamWriter(gzip, StandardCharsets.UTF_8);
osw.write(saves.toJSONString());
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
After some search and try I learned how to use it properly and without using my folder data. I'd want to share my example code then people who search for that.
Here's the example code
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream);
gzip.write(saves.toJSONString().getBytes(StandardCharsets.UTF_8));
gzip.close();
GridFSBucket gridFSFilesBucket = GridFSBuckets.create(database, "pdisks");
GridFSUploadOptions options = new GridFSUploadOptions()
.metadata(new Document("metadataValue", "test-value"));
InputStream ff = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectId fileId = gridFSFilesBucket.uploadFromStream(minewatchSaveManager.getId(), ff, options);