Search code examples
google-app-enginegomultipart

Parse multipart form on Google App Engine


A project I'm working on depends on having a service hosted on Google App Engine parse from SendGrid. The following code is an example of what we're doing:

package sendgrid_failure

import (
    "net/http"
    "fmt"
    "google.golang.org/appengine"
    "google.golang.org/appengine/log"
)

func init() {
    http.HandleFunc("/sendgrid/parse", sendGridHandler)
}

func sendGridHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
    err := r.ParseMultipartForm(-1)
    if err != nil {
        log.Errorf(ctx, "Unable to parse form: %v", err)
    }

    fmt.Fprint(w, "Test.")
}

When SendGrid POSTs its multipart form, the console shows similar to the following:

2018/01/04 23:44:08 ERROR: Unable to parse form: open /tmp/multipart-445139883: no file writes permitted on App Engine

App Engine doesn't allow you to read/write files, but Golang appears to need it to parse. Is there an App Engine specific library to parse multipart forms, or should we be using a different method from the standard net/http library entirely? We're using the standard go runtime.


Solution

  • The documentation for ParseMultipartForm says:

    The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files.

    The server attempts to write all files to disk because the application passed -1 as maxMemory. Use a value larger than the size of the files you expect to upload.