doudiemei2013 2015-03-26 18:01
浏览 73
已采纳

Google App Engine Golang没有这样的文件或目录

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条回答 默认 最新

  • douhaoqiao9304 2015-03-26 21:05
    关注

    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:

    1) 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):

    2) 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持