I've recently started playing a bit with AppEngine but I'm having problems opening a file. Here's the code I'm using:
if _, err := os.Open("/pizza.webp"); err != nil {
printError(err.Error())
}
This gives me the error: open /pizza.webp: operation not permitted
I've tried using a .png instead and go the same result. I've also tried without the slash and with a dot before the slash, both resulted in the error no such file or directory
so I'm guessing I have the path right but for some reason I don't have the permission to access it, maybe there's something I need to write in app.yaml? Right now app.yaml looks like this:
application: pizzarobot-telegram
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
Which is the default app.yaml with my application id. I've tried setting a static directory through app.yaml but that didn't work neither, I've read that AppEngine stores your static files apart from the code in that situation.
I'm also very new to Go so I might be doing this wrong way and might not be an AppEngine problem but I've used os.Open in the past without AppEngine and that worked so I don't know what I'm missing here.
It should work without the slash. File paths are relative to your project root (where your app.yaml
is).
I just tried this with 3 files:
main.go
app.yaml
pizza.txt
main.go
:
package main
import (
"io"
"net/http"
"os"
)
func init() {
http.HandleFunc("/pizza.txt", func(res http.ResponseWriter, req *http.Request) {
f, err := os.Open("pizza.txt")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer f.Close()
io.Copy(res, f)
})
}
pizza.txt
:
Totally Works!
app.yaml
:
application: astute-curve-100822
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
It ran locally and on app engine.