Class GridFsTemplate
java.lang.Object
org.springframework.data.mongodb.gridfs.GridFsTemplate
- All Implemented Interfaces:
ResourceLoader
,ResourcePatternResolver
,GridFsOperations
GridFsOperations
implementation to store content into MongoDB GridFS.- Author:
- Oliver Gierke, Philipp Schneider, Thomas Darimont, Martin Baumgartner, Christoph Strobl, Mark Paluch, Hartmut Lang, Niklas Helge Hanft, Denis Zavedeev
-
Field Summary
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
-
Constructor Summary
ConstructorsConstructorDescriptionGridFsTemplate
(MongoConverter converter, Supplier<com.mongodb.client.gridfs.GridFSBucket> gridFSBucket) Creates a newGridFsTemplate
using the givenMongoConverter
andSupplier
providing the requiredGridFSBucket
.GridFsTemplate
(MongoDatabaseFactory dbFactory, MongoConverter converter) GridFsTemplate
(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) -
Method Summary
Modifier and TypeMethodDescriptionprotected com.mongodb.client.gridfs.model.GridFSUploadOptions
computeUploadOptionsFor
(@Nullable String contentType, @Nullable org.bson.Document metadata) Compute theGridFSUploadOptions
to be used from the given contentType and metadataDocument
.void
Deletes all files matching the givenQuery
.com.mongodb.client.gridfs.GridFSFindIterable
Returns all files matching the given query.com.mongodb.client.gridfs.model.GridFSFile
Returns a singleGridFSFile
matching the given query or null in case no file matches.@Nullable ClassLoader
protected org.bson.Document
getMappedQuery
(org.bson.Document query) getResource
(com.mongodb.client.gridfs.model.GridFSFile file) Returns theGridFsResource
for aGridFSFile
.getResource
(String location) Returns theGridFsResource
with the given file name.getResources
(String locationPattern) Returns allGridFsResource
s matching the given file name pattern.org.bson.types.ObjectId
store
(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) Stores the given content into a file with the given name and content type using the given metadata.<T> T
store
(GridFsObject<T, InputStream> upload) protected org.bson.Document
toDocument
(@Nullable Object value) Convert a given value into aDocument
.
-
Constructor Details
-
GridFsTemplate
Creates a newGridFsTemplate
using the givenMongoDatabaseFactory
andMongoConverter
.Note that the
GridFSBucket
is obtained only once fromMongoDatabase
. UseGridFsTemplate(MongoConverter, Supplier)
if you want to use different buckets from the same Template instance.- Parameters:
dbFactory
- must not be null.converter
- must not be null.
-
GridFsTemplate
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) Creates a newGridFsTemplate
using the givenMongoDatabaseFactory
andMongoConverter
.Note that the
GridFSBucket
is obtained only once fromMongoDatabase
. UseGridFsTemplate(MongoConverter, Supplier)
if you want to use different buckets from the same Template instance.- Parameters:
dbFactory
- must not be null.converter
- must not be null.bucket
- can be null.
-
GridFsTemplate
public GridFsTemplate(MongoConverter converter, Supplier<com.mongodb.client.gridfs.GridFSBucket> gridFSBucket) Creates a newGridFsTemplate
using the givenMongoConverter
andSupplier
providing the requiredGridFSBucket
.- Parameters:
converter
- must not be null.gridFSBucket
- must not be null.- Since:
- 4.2
-
-
Method Details
-
store
public org.bson.types.ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) Description copied from interface:GridFsOperations
Stores the given content into a file with the given name and content type using the given metadata. The metadata object will be marshalled before writing.- Specified by:
store
in interfaceGridFsOperations
- Parameters:
content
- must not be null.filename
- must not be null or empty.contentType
- can be null.metadata
- can be null- Returns:
- the
ObjectId
of theGridFSFile
just created.
-
store
Description copied from interface:GridFsOperations
Stores the givenGridFsObject
, likely aGridFsUpload
, into into a file with givenname
. If theGridFsObject.getFileId()
is set, the file will be stored with that id, otherwise the server auto creates a new id.- Specified by:
store
in interfaceGridFsOperations
- Type Parameters:
T
- id type of the underlyingGridFSFile
- Parameters:
upload
- theGridFsObject
(most likely aGridFsUpload
) to be stored.- Returns:
- the id of the stored file. Either an auto created value or
GridFsObject.getFileId()
, but never null.
-
find
Description copied from interface:GridFsOperations
Returns all files matching the given query. Note, that currentlySort
criterias defined at theQuery
will not be regarded as MongoDB does not support ordering for GridFS file access.- Specified by:
find
in interfaceGridFsOperations
- Parameters:
query
- must not be null.- Returns:
GridFSFindIterable
to obtain results from. Eg. by callingMongoIterable.into(java.util.Collection)
.- See Also:
-
findOne
Description copied from interface:GridFsOperations
Returns a singleGridFSFile
matching the given query or null in case no file matches.- Specified by:
findOne
in interfaceGridFsOperations
- Parameters:
query
- must not be null.- Returns:
- can be null.
-
delete
Description copied from interface:GridFsOperations
Deletes all files matching the givenQuery
.- Specified by:
delete
in interfaceGridFsOperations
- Parameters:
query
- must not be null.
-
getClassLoader
- Specified by:
getClassLoader
in interfaceResourceLoader
-
getResource
Description copied from interface:GridFsOperations
Returns theGridFsResource
with the given file name.- Specified by:
getResource
in interfaceGridFsOperations
- Specified by:
getResource
in interfaceResourceLoader
- Parameters:
location
- must not be null.- Returns:
- the resource. Use
Resource.exists()
to check if the returnedGridFsResource
is actually present. - See Also:
-
getResource
Description copied from interface:GridFsOperations
Returns theGridFsResource
for aGridFSFile
.- Specified by:
getResource
in interfaceGridFsOperations
- Parameters:
file
- must not be null.- Returns:
- the resource for the file.
-
getResources
Description copied from interface:GridFsOperations
Returns allGridFsResource
s matching the given file name pattern.- Specified by:
getResources
in interfaceGridFsOperations
- Specified by:
getResources
in interfaceResourcePatternResolver
- Parameters:
locationPattern
- must not be null.- Returns:
- an empty array if none found.
- See Also:
-
getMappedQuery
protected org.bson.Document getMappedQuery(org.bson.Document query) - Parameters:
query
- pass the given query though aQueryMapper
to apply type conversion.- Returns:
- never null.
-
computeUploadOptionsFor
protected com.mongodb.client.gridfs.model.GridFSUploadOptions computeUploadOptionsFor(@Nullable String contentType, @Nullable org.bson.Document metadata) Compute theGridFSUploadOptions
to be used from the given contentType and metadataDocument
.- Parameters:
contentType
- can be null.metadata
- can be null- Returns:
- never null.
-
toDocument
Convert a given value into aDocument
.- Parameters:
value
- can be null.- Returns:
- an empty
Document
if the source value is null.
-