英文:
Google App Engine Golang no such file or directory
问题
我正在开发一个使用Go语言的Google App Engine项目,在读取文件时遇到了问题。
实际上,该应用在本地运行时完全正常。但是当部署后,它会抛出恐慌错误,告诉我找不到该文件或目录。
这是我的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,
有任何想法为什么会发生这种情况以及如何修复吗?
提前感谢
英文:
I am developing a Google App Engine project in go and got stuck at reading files.
In fact app works perfectly locally. However when deployed, it panics telling me that there is no such file or directory.
Here is my fileValue method:
func fileValue(path string) string {
content, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return string(content)
}
And I call it like this:
secondPart := fileValue("./console/page/secondPart.html")
And this is what I can se under logs in developers console:
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,
Any idea why this happens and how to fix it?
Thanks in advance
答案1
得分: 4
当您上传/部署应用程序时,应用程序文件和静态文件被分别存储。静态文件由专用服务器提供,而不是由前端实例提供。
这意味着如果您有一个文件想要从Go代码中读取,那个文件不能与任何静态文件模式匹配,并且不能在指定为静态目录的文件夹中,否则该文件将被视为静态文件,并且不会与您的Go代码一起部署。
这在应用程序配置页面的静态文件处理程序部分有详细说明。引用相关部分:
> 为了提高效率,App Engine将静态文件与应用程序文件分别存储和提供。静态文件在应用程序的文件系统中不可用。如果您有需要应用程序代码读取的数据文件,这些数据文件必须是应用程序文件,并且不能与静态文件模式匹配。
如果有一个文件既要作为静态文件又要作为应用程序文件,您有两个选项:
1)您可以复制它,例如将其放在代码旁边并放在一个单独的文件夹中(例如static
),您可以将其标记为静态目录。
或者(首选):
2)为包含/适用于该文件的静态文件处理程序指定application_readable
选项。引用文档中的说明:
> 可选。默认情况下,静态文件处理程序中声明的文件被上传为静态数据,并且仅提供给最终用户,应用程序无法读取它们。如果将此字段设置为true,则文件也将作为代码数据上传,以便您的应用程序可以读取它们。这两个上传都计入您的代码和静态数据存储资源配额。
英文:
When you upload/deploy your application, application files and static files are stored separately. Static files are served by specialized/dedicated servers, not by your frontend instances.
That means if you have a file which you want to read from your Go code, that file must not match any static file pattern and cannot be in a folder specified as a static directory, else the file will be considered a static file and will not be deployed next to your Go code.
This is detailed on the Application configuration page, Section Static file handlers. Quoting the relevant part:
> 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.
If there is a file which you want to be both a static file and an application file, you have 2 options:
- You may duplicate it, e.g. put it next to your code and to a separate folder (e.g.
static
) which you can mark as a static dir.
Or (preferred):
- Specify the
application_readable
option to the static file handler which includes/applies to the file. Quoting from the documentation:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论