DEV Community

loading...

SpringData MongoDB GridFsTemplate to save, retrieve, delete binary files (Image, Text files)

loizenai profile image loizenai ・2 min read

https://grokonez.com/spring-framework/spring-data/springdata-mongodb-gridfstemplate-save-retrieve-delete-binary-files-image-text-files

SpringData MongoDB GridFsTemplate to save, retrieve, delete binary files (Image, Text files)

MongoDB provides GridFS for storing and retrieving large files with size limit of 16MB. So in the tutorial, JavaSampleApproach will show you how to work with SpringData Mongo GridFsTemplate to save, retrieve, delete binary files (Image, Text files).

Related posts:

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.7.RELEASE
– MongoDB: version v3.4.9

II. SpringData Mongo GridFsTemplate

SpringData MongoDb provides GridFsTemplate class to work with GridFs file, details:


public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
...

}

GridFsTemplate implement interface GridFsOperations that is the collection of operations to store and read files from MongoDB GridFS.

For convenient working, SpringBoot provides spring-boot-starter-data-mongodb dependency that is the collection of {mongodb-driver, spring-data-mongodb}.

III. Practice

In the tutorial, we create a SpringBoot project as below:

SpringData Mongo GridFs - project structure

Step to do:

  • Create SpringBoot project
  • Configure GridFsTemplate
  • Implement RestAPIs to work with GridFs files
  • Run & check results

    1. Create SpringBoot project

More at:

https://grokonez.com/spring-framework/spring-data/springdata-mongodb-gridfstemplate-save-retrieve-delete-binary-files-image-text-files

Discussion (0)

pic
Editor guide