2018-06-05 11:22 阅读 184


I am working on a go project where I need to serve files stored in mongodb. The files are stored in a GridFs. I use as package to connect and query the db.

I can retrieve the file from the db, that is not hard.

f, err := s.files.OpenId(id)

But how can I serve that file with http? I work with the JulienSchmidt router to handle all the other restfull requests. The solutions I find always use static files, not files from a db.

Thanks in advance

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanbishai5271 duanbishai5271 2018-06-05 11:32

    Tip: Recommended to use instead of (the latter is not maintained anymore).

    The mgo.GridFile type implements io.Reader, so you could use io.Copy() to copy its content into the http.ResponseWriter.

    But since mgo.GridFile also implements io.Seeker, you may take advantage of http.ServeContent(). Quoting its doc:

    The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.

    Example handler serving a file:

    func serveFromDB(w http.ResponseWriter, r *http.Request) {
        var gridfs *mgo.GridFS // Obtain GridFS via Database.GridFS(prefix)
        name := "somefile.pdf"
        f, err := gridfs.Open(name)
        if err != nil {
            log.Printf("Failed to open %s: %v", name, err)
            http.Error(w, "something went wrong", http.StatusInternalServerError)
        defer f.Close()
        http.ServeContent(w, r, name, time.Now(), f) // Use proper last mod time
    点赞 评论 复制链接分享