I can\'t figure out how to stream a binary file from GridFS with spring-data-mongodb and its GridFSTemplate
when I already have the right ObjectId
.
Have you considered using Spring Content for Mongo for the content storage piece on your solution?
Assuming you are using Spring Boot as well as Spring Data Mongo then it might look something like the following:
pom.xml
com.github.paulcwarren
spring-content-mongo-boot-starter
0.0.10
com.github.paulcwarren
spring-content-rest-boot-starter
0.0.10
Update your Spring Data Mongo entity, with the following attributes:
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
@MimeType
private String mimeType;
Add a store interface:
@StoreRestResource(path="content")
public interface MongoContentStore extends ContentStore {
}
That's all that you need. When you application starts Spring Content will see the dependencies on the Spring Content Mongo/REST modules and it will inject an implementation of the MongonContenStore
store for GridFs as well as an implementation of a controller that supports full CRUD functionality and maps those operations down onto the underlying store interface. The REST endpoint will be available under /content
.
i.e.
curl -X PUT /content/{entityId}
will create or update an entity's image
curl -X GET /content/{entityId}
will fetch the entity's image
curl -X DELETE /content/{entityId}
will delete the entity's image
There are a couple of getting started guides here. They use Spring Content for the filesystem but the modules are interchangeable. The Mongo reference guide is here. And there is a tutorial video here.
HTH