英文:
os.Open on AppEngine with Go
问题
我最近开始尝试使用AppEngine,但在打开文件时遇到了问题。以下是我使用的代码:
if _, err := os.Open("/pizza.webp"); err != nil {
printError(err.Error())
}
这给我返回了错误:open /pizza.webp: operation not permitted
。
我尝试使用了一个.png文件,结果也是一样的。我还尝试过去掉斜杠和在斜杠前加一个点,但都返回了错误no such file or directory
,所以我猜测路径应该是正确的,但由于某种原因我没有权限访问它,也许我需要在app.yaml中写入一些内容?目前app.yaml的内容如下:
application: pizzarobot-telegram
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
这是带有我的应用程序ID的默认app.yaml。我尝试通过app.yaml设置一个静态目录,但也没有成功,我读到过在这种情况下,AppEngine会将静态文件与代码分开存储。
我对Go也非常陌生,所以我可能做错了什么,也可能不是AppEngine的问题,但我以前在没有AppEngine的情况下使用过os.Open,那时是可以工作的,所以我不知道我在这里漏掉了什么。
英文:
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.
答案1
得分: 2
它应该可以在没有斜杠的情况下工作。文件路径是相对于项目根目录的(即app.yaml
所在的位置)。
我刚刚尝试了以下三个文件:
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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论