You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MongoDB supports storing binary files inside its filesystem, GridFS.
490
+
Spring Data MongoDB provides a `ReactiveGridFsOperations` interface as well as the corresponding implementation, `ReactiveGridFsTemplate`, to let you interact with the filesystem.
491
+
You can set up a `ReactiveGridFsTemplate` instance by handing it a `ReactiveMongoDatabaseFactory` as well as a `MongoConverter`, as the following example shows:
492
+
493
+
.JavaConfig setup for a ReactiveGridFsTemplate
494
+
====
495
+
[source,java]
496
+
----
497
+
class GridFsConfiguration extends AbstractReactiveMongoConfiguration {
498
+
499
+
// … further configuration omitted
500
+
501
+
@Bean
502
+
public ReactiveGridFsTemplate reactiveGridFsTemplate() {
503
+
return new ReactiveGridFsTemplate(reactiveMongoDbFactory(), mappingMongoConverter());
504
+
}
505
+
}
506
+
----
507
+
====
508
+
509
+
The template can now be injected and used to perform storage and retrieval operations, as the following example shows:
510
+
511
+
.Using ReactiveGridFsTemplate to store files
512
+
====
513
+
[source,java]
514
+
----
515
+
class ReactiveGridFsClient {
516
+
517
+
@Autowired
518
+
ReactiveGridFsTemplate operations;
519
+
520
+
@Test
521
+
public Mono<ObjectId> storeFileToGridFs() {
522
+
523
+
FileMetadata metadata = new FileMetadata();
524
+
// populate metadata
525
+
Publisher<DataBuffer> file = … // lookup File or Resource
The `store(…)` operations take an `Publisher<DataBuffer>`, a filename, and (optionally) metadata information about the file to store. The metadata can be an arbitrary object, which will be marshaled by the `MongoConverter` configured with the `ReactiveGridFsTemplate`. Alternatively, you can also provide a `Document`.
534
+
535
+
NOTE: MongoDB's driver uses `AsyncInputStream` and `AsyncOutputStream` interfaces to exchange binary streams. Spring Data MongoDB adapts these interfaces to `Publisher<DataBuffer>`. Read more about `DataBuffer` in http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#databuffers[Spring's reference documentation].
536
+
537
+
You can read files from the filesystem through either the `find(…)` or the `getResources(…)` methods. Let's have a look at the `find(…)` methods first. You can either find a single file or multiple files that match a `Query`. You can use the `GridFsCriteria` helper class to define queries. It provides static factory methods to encapsulate default metadata fields (such as `whereFilename()` and `whereContentType()`) or a custom one through `whereMetaData()`. The following example shows how to use `ReactiveGridFsTemplate` to query for files:
NOTE: Currently, MongoDB does not support defining sort criteria when retrieving files from GridFS. For this reason, any sort criteria defined on the `Query` instance handed into the `find(…)` method are disregarded.
557
+
558
+
The other option to read files from the GridFs is to use the methods modeled along the lines of `ResourcePatternResolver`.
559
+
`ReactiveGridFsOperations` uses reactive types to defer execution while `ResourcePatternResolver` uses a synchronous interface.
560
+
These methods allow handing an Ant path into the method and can thus retrieve files matching the given pattern. The following example shows how to use `ReactiveGridFsTemplate` to read files:
0 commit comments