How to get a binary stream by GridFS ObjectId with Spring Data MongoDB

前端 未结 10 1242
暗喜
暗喜 2021-02-02 15:00

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.

10条回答
  •  情歌与酒
    2021-02-02 15:21

    Old question I know, but trying to do this in 2019 using WebFlux, I had to do the following

      public Mono getImageFromDatabase(final String id) {
    
        return Mono.fromCallable(
            () ->
                this.gridFsTemplate.getResource(
                    Objects.requireNonNull(
                            this.gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))))
                        .getFilename()));
      }
    

    Which will give you a Mono which can be returned in a controller. I'm sure there is a nicer solution however.

提交回复
热议问题