diff --git a/README.md b/README.md index 651be259..92415da9 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,13 @@ We have separate folders for the samples of individual modules: * `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics. * `fluent-api` - Example project to show the new fluent API (`MongoTemplate`-alternative) to interact with MongoDB. * `geo-json` - Example project showing usage of [GeoJSON](http://geojson.org) with MongoDB. +* `gridfs` - Example project showing usage of gridFS with MongoDB. * `java8` - Example of how to use Spring Data MongoDB with Java 8 date time types as well as the usage of `Optional` as return type for repository methods. Note, this project requires to be build with JDK 8. * `query-by-example` - Example project showing usage of Query by Example with MongoDB. * `reactive` - Example project to show reactive template and repository support. * `security` - Example project showing usage of Spring Security with MongoDB. * `text-search` - Example project showing usage of MongoDB text search feature. - ## Spring Data REST * `headers` - A sample showing the population of HTTP headers and the usage of them to perform conditional `GET` requests. diff --git a/mongodb/gridfs/README.md b/mongodb/gridfs/README.md new file mode 100644 index 00000000..9c9570f9 --- /dev/null +++ b/mongodb/gridfs/README.md @@ -0,0 +1,39 @@ +# Spring Data MongoDB - GridFS example + +This project contains an example of [GridFS](https://docs.mongodb.com/v3.4/core/gridfs/) specific features of Spring Data (MongoDB). + +## Support for storing a file + +Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to store a file. + +```java +InputStream is = ... +gridFsOperations.store(is, "myFile1.txt"); +``` + +## Support for query a file by name + +Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to query a GridFSDBFile by its filename. + +```java +GridFSDBFile gridFsFile = gridFsOperations.findOne(query(whereFilename().is("myFile1.txt"))); +``` + +## Support for storing a file with metadata + +Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to store a file with additional metadata.. + +```java +InputStream is = ... +Customer customerMetaData = new Customer("Hardy", "Lang"); +gridFsOperations.store(is, "myCustomerFile.txt", customerMetaData); + +``` + +## Support for query a file by metadata + +Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to query a GridFSDBFile by metadata. + +```java +GridFSDBFile gridFsFile = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy"))); +``` diff --git a/mongodb/gridfs/pom.xml b/mongodb/gridfs/pom.xml new file mode 100644 index 00000000..5aaf8b42 --- /dev/null +++ b/mongodb/gridfs/pom.xml @@ -0,0 +1,16 @@ + + 4.0.0 + + spring-data-mongodb-gridfs + + Spring Data MongoDB - GridFs + + + org.springframework.data.examples + spring-data-mongodb-examples + 2.0.0.BUILD-SNAPSHOT + + + + diff --git a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java new file mode 100644 index 00000000..f702950e --- /dev/null +++ b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/Customer.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package example.springdata.mongodb.gridfs; + +import org.springframework.data.annotation.Id; + +public class Customer { + + @Id + public String id; + + public String firstName; + public String lastName; + + public Customer() {} + + public Customer(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + + public String getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + @Override + public String toString() { + return String.format( + "Customer[id=%s, firstName='%s', lastName='%s']", + id, firstName, lastName); + } + +} \ No newline at end of file diff --git a/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java new file mode 100644 index 00000000..cbf428ae --- /dev/null +++ b/mongodb/gridfs/src/main/java/example/springdata/mongodb/gridfs/GridFsApplication.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package example.springdata.mongodb.gridfs; + +import com.mongodb.client.gridfs.model.GridFSFile; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.mongodb.gridfs.GridFsOperations; +import org.springframework.data.mongodb.gridfs.GridFsResource; +import org.springframework.util.StreamUtils; + +import java.io.BufferedInputStream; +import java.io.InputStream; + +import static org.springframework.data.mongodb.core.query.Query.query; +import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename; +import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereMetaData; + +/** + * Spring Boot sample application to show the usage of {@link GridFsOperations} with Spring Data MongoDB. + * + * @author Hartmut Lang + */ +@SpringBootApplication +public class GridFsApplication implements CommandLineRunner { + + @Autowired + private GridFsOperations gridFsOperations; + + public static void main(String[] args) { + SpringApplication.run(GridFsApplication.class, args); + } + + @Override + public void run(String... strings) throws Exception { + + // store file + try (InputStream is = new BufferedInputStream(new ClassPathResource("./myFile1.txt").getInputStream())) { + gridFsOperations.store(is, "myFile1.txt"); + } + + // get file or resource by filename + GridFSFile gridFsFile1 = gridFsOperations.findOne(query(whereFilename().is("myFile1.txt"))); + System.out.println("Filename: " + gridFsFile1.getFilename() +", MD5: " + gridFsFile1.getMD5()); + GridFsResource gridRes1 = gridFsOperations.getResource("myFile1.txt"); + StreamUtils.copy(gridRes1.getInputStream(), System.out); + + // store file with metaData + try (InputStream is = new BufferedInputStream(new ClassPathResource("./myCustomerFile.txt").getInputStream())) { + Customer customerMetaData = new Customer("Hardy", "Lang"); + gridFsOperations.store(is, "myCustomerFile.txt", customerMetaData); + } + + // search by metaData + GridFSFile gridFsFile2 = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy"))); + System.out.println("Filename: " + gridFsFile2.getFilename() +", MD5: " + gridFsFile2.getMD5()); + GridFsResource gridRes2 = gridFsOperations.getResource(gridFsFile2.getFilename()); + StreamUtils.copy(gridRes2.getInputStream(), System.out); + } +} diff --git a/mongodb/gridfs/src/main/resources/application.properties b/mongodb/gridfs/src/main/resources/application.properties new file mode 100644 index 00000000..0d039a4a --- /dev/null +++ b/mongodb/gridfs/src/main/resources/application.properties @@ -0,0 +1 @@ +#spring.data.mongodb.grid-fs-database=dbfs diff --git a/mongodb/gridfs/src/main/resources/myCustomerFile.txt b/mongodb/gridfs/src/main/resources/myCustomerFile.txt new file mode 100644 index 00000000..5e0e3844 --- /dev/null +++ b/mongodb/gridfs/src/main/resources/myCustomerFile.txt @@ -0,0 +1,2 @@ +This is my example file. +It gets added as file to gridFs. diff --git a/mongodb/gridfs/src/main/resources/myFile1.txt b/mongodb/gridfs/src/main/resources/myFile1.txt new file mode 100644 index 00000000..0e380079 --- /dev/null +++ b/mongodb/gridfs/src/main/resources/myFile1.txt @@ -0,0 +1 @@ +Just a simple file to add to GridFS diff --git a/mongodb/pom.xml b/mongodb/pom.xml index c8229f5d..1991fec3 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -26,6 +26,7 @@ query-by-example reactive fluent-api + gridfs