google-app-engine - Google App Engine Golang 没有这样的文件或目录

标签 google-app-engine go

我正在 go 中开发一个 Google App Engine 项目,但在读取文件时卡住了。 事实上,应用程序在本地完美运行。但是当部署时,它会 panic 告诉我没有这样的文件或目录。

这是我的 fileValue 方法:

func fileValue(path string) string {
    content, err := ioutil.ReadFile(path)
    if err != nil {
        panic(err)
    }
    return string(content)
}

我这样调用它:

secondPart := fileValue("./console/page/secondPart.html")

这是我在开发人员控制台的日志下可以看到的内容:

panic: open ./console/page/firstPart.html: no such file or directory

goroutine 11 [running]:
console.fileValue(0x19582f0, 0x1d, 0x0, 0x0)
    console/console.go:191 +0xbd
console.generateUnsignedHtml(0xc01043a780, 0x0, 0x0)
    console/console.go:68 +0x69
console.consoleHandler(0x7f180fa61830, 0xc01042f380, 0xc0105640d0)
    console/console.go:58 +0x37e
net/http.HandlerFunc.ServeHTTP(0x1a21210, 0x7f180fa61830, 0xc01042f380, 0xc0105640d0)
    go/src/net/http/server.go:1265 +0x56
net/http.(*ServeMux).ServeHTTP(0xc01048a8a0, 0x7f180fa61830, 0xc01042f380, 0xc0105640d0)
    go/src/net/http/server.go:1541 +0x1b4
appengine_internal.executeRequestSafely(0xc01042f380, 0xc0105640d0)
    go/src/appengine_internal/api_prod.go:280 +0xb7
appengine_internal.(*server).HandleRequest(0x1be76f0, 0xc010540000, 0xc0104ba000, 0xc010430b60, 0x0, 0x0)
    go/src/appengine_internal/api_prod.go:214 +0x102b
reflect.Value.call(0x1842640, 0x1be76f0, 0x113, 0x18d1380, 0x4, 0xc010533f78, 0x3, 0x3, 0x0, 0x0, ...)
    /tmp/appengine/go/src/reflect/value.go:419 +0x10fd
reflect.Value.Call(0x1842640, 0x1be76f0, 0x113, 0xc010533f78, 0x3, 

知道为什么会发生这种情况以及如何解决它吗?

提前致谢:)

最佳答案

当您上传/部署您的应用程序时,应用程序文件和静态文件是分开存储的。静态文件由专用/专用服务器提供,而不是由您的前端实例提供。

这意味着如果你有一个文件要从你的 Go 代码中读取,该文件不能匹配任何静态文件模式并且不能位于指定为静态目录的文件夹中,否则该文件将被视为静态文件并且不会部署在您的 Go 代码旁边。

这在 Application configuration 上有详细说明页,第 Static file handlers .引用相关部分:

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system. If you have data files that need to be read by the application code, the data files must be application files, and must not be matched by a static file pattern.

如果您希望某个文件既是静态文件又是应用程序文件,您有两个选择:

1) 你可以复制它,例如将它放在您的代码旁边和一个单独的文件夹(例如 static),您可以将其标记为静态目录。

或者(首选):

2) 为包含/应用于文件的静态文件处理程序指定application_readable 选项。引用文档:

Optional. By default, files declared in static file handlers are uploaded as static data and are only served to end users, they cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.

关于google-app-engine - Google App Engine Golang 没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285670/

相关文章:

go - 如果我要返回变量的副本而不是指针,是否需要互斥体?

python - 获取 ndb 模型实例的属性,其中 Python 名称!= 数据存储名称

java - 将 Zip 文件写入 GAE Blobstore

node.js - Node 应用程序在 Google Cloud App Engine 上监听低于 1024 的端口

json - 在golang中用反斜杠解析JSON

go - 修复 go 使用的工具版本

java - JPA 和安卓

python - 使用python连接cloudsql出错

go - 我不能 "go get"来自 github 的依赖

go - 全局标志和子命令