mongodbgogridfsfileserver

How to serve a file from mongodb with golang


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 gopkg.in/mgo.v2 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


Solution

  • Tip: Recommended to use github.com/globalsign/mgo instead of gopkg.in/mgo.v2 (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)
            return
        }
        defer f.Close()
    
        http.ServeContent(w, r, name, time.Now(), f) // Use proper last mod time
    }