apigogoogle-cloud-platformhosted

Hosted api on google cloud issue with file paths


I have an image file in the root directory with the main.go file. How would I get the path to serve the file while the golang api is hosted on google cloud (seems like all the files get messed up when its hosted on google cloud). Heres the code im using now:

func ServeImage(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)

    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }
    executableDir := filepath.Dir(ex)

    //TODO error
    file, err := os.Open(path.Join(executableDir, "/"+params["name"]))
    if (err != nil) {
        http.Error(w, err.Error(), http.StatusBadRequest)
        fmt.Println(err)
    }
    defer file.Close()
    http.ServeContent(w, r, "image", time.Now(), file)
}

The error i get from this is:

open /layers/google.go.build/bin/_DSC7451.jpeg: no such file or directory
seeker can't seek

Solution

  • If you are using app engine then you don't really have access to file system. https://cloud.google.com/appengine/docs/standard/runtimes

    You should be using cloud storage to write and read files. That will allow your files to be accessible across all the instances of your service that may be running at any given time.