对于最新的稳定版本,请使用 Spring Data MongoDB 4.4.0! |
GridFS 支持
MongoDB 支持将二进制文件存储在其文件系统 GridFS 中。
Spring Data MongoDB 提供了一个GridFsOperations
和ReactiveGridFsOperations
interface 以及相应的实现,GridFsTemplate
和ReactiveGridFsTemplate
,以便与文件系统交互。
您可以通过向模板实例发送MongoDatabaseFactory
/ReactiveMongoDatabaseFactory
以及MongoConverter
,如下例所示:
-
Imperative
-
Reactive
-
XML
class GridFsConfiguration extends AbstractMongoClientConfiguration {
// … further configuration omitted
@Bean
public GridFsTemplate gridFsTemplate() {
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
}
}
class ReactiveGridFsConfiguration extends AbstractReactiveMongoConfiguration {
// … further configuration omitted
@Bean
public ReactiveGridFsTemplate reactiveGridFsTemplate() {
return new ReactiveGridFsTemplate(reactiveMongoDbFactory(), mappingMongoConverter());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<mongo:db-factory id="mongoDbFactory" dbname="database" />
<mongo:mapping-converter id="converter" />
<bean class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg ref="converter" />
</bean>
</beans>
现在可以注入模板并用于执行存储和检索作,如下例所示:
-
Imperative
-
Reactive
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void storeFileToGridFs() {
FileMetadata metadata = new FileMetadata();
// populate metadata
Resource file = … // lookup File or Resource
operations.store(file.getInputStream(), "filename.txt", metadata);
}
}
这store(…)
作采用InputStream
、文件名和(可选)有关要存储的文件的元数据信息。
元数据可以是任意对象,该对象将由MongoConverter
配置了GridFsTemplate
.
或者,您也可以提供Document
.
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsTemplate operations;
@Test
public Mono<ObjectId> storeFileToGridFs() {
FileMetadata metadata = new FileMetadata();
// populate metadata
Publisher<DataBuffer> file = … // lookup File or Resource
return operations.store(file, "filename.txt", metadata);
}
}
这store(…)
作采用Publisher<DataBuffer>
、文件名和(可选)有关要存储的文件的元数据信息。
元数据可以是任意对象,该对象将由MongoConverter
配置了ReactiveGridFsTemplate
.
或者,您也可以提供Document
.
MongoDB 的驱动程序使用AsyncInputStream
和AsyncOutputStream
用于交换二进制流的接口。
Spring Data MongoDB 将这些接口调整为Publisher<DataBuffer>
.
阅读更多DataBuffer
在 Spring 的参考文档中。
您可以通过find(…)
或getResources(…)
方法。
我们来看看find(…)
方法。
您可以找到单个文件或多个文件,这些文件与Query
.
您可以使用GridFsCriteria
helper 类来定义查询。
它提供了静态工厂方法来封装默认元数据字段(例如whereFilename()
和whereContentType()
) 或自定义whereMetaData()
.
以下示例演示如何使用模板查询文件:
-
Imperative
-
Reactive
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void findFilesInGridFs() {
GridFSFindIterable result = operations.find(query(whereFilename().is("filename.txt")));
}
}
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsTemplate operations;
@Test
public Flux<GridFSFile> findFilesInGridFs() {
return operations.find(query(whereFilename().is("filename.txt")))
}
}
目前,MongoDB 不支持在从 GridFS 检索文件时定义排序条件。因此,在Query 实例传递到find(…) 方法被忽略。 |
从 GridFs 读取文件的另一个选项是使用ResourcePatternResolver
接口。
它们允许将 Ant 路径传递到方法中,因此可以检索与给定模式匹配的文件。
以下示例演示如何使用GridFsTemplate
要读取文件:
-
Imperative
-
Reactive
class GridFsClient {
@Autowired
GridFsOperations operations;
public GridFsResources[] readFilesFromGridFs() {
return operations.getResources("*.txt");
}
}
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsOperations operations;
public Flux<ReactiveGridFsResource> readFilesFromGridFs() {
return operations.getResources("*.txt");
}
}
GridFsOperations
延伸ResourcePatternResolver
并让GridFsTemplate
(例如)插入ApplicationContext
从 MongoDB 数据库中读取 Spring Config 文件。
默认情况下,GridFsTemplate 获得GridFSBucket 第一次 GridFS 交互时。
之后,模板实例将重用缓存的存储桶。
要使用不同的存储桶,请从同一个 Template 实例使用接受Supplier<GridFSBucket> . |