Correct way to read file in Go AppEngine

huangapple go评论69阅读模式
英文:

Correct way to read file in Go AppEngine

问题

在使用Google AppEngine(Go)时,正确的读取文件的方法是什么?

在Java中,我知道可以使用context.getResourceAsStream来读取文件,那么在Go中有没有类似的函数呢?

英文:

What's the correct way to read a file using Google AppEngine (Go)?

In Java I read there are context.getResourceAsStream, is there any equivalent function for that?

答案1

得分: 7

您可以像在计算机上运行的Go应用程序中读取文件一样,在App Engine上读取文件。

需要注意以下几点:

  • 您应该使用相对文件路径而不是绝对路径。工作目录是您的应用程序的根文件夹(即app.yaml文件所在的位置)。

  • 只有_应用程序_文件可以被Go代码读取,因此如果您想从Go代码中读取文件,该文件不能与静态文件模式匹配(或者如果它也必须作为静态文件可用,则必须在包含/应用于该文件的静态文件处理程序中指定application_readable选项,详情)。

后者在应用程序配置页面的静态文件处理程序部分有详细说明。引用相关部分:

> 为了提高效率,App Engine将静态文件与应用程序文件分开存储和提供。静态文件在应用程序的文件系统中不可用。如果您有需要应用程序代码读取的数据文件,这些数据文件必须是应用程序文件,并且不能与静态文件模式匹配。

假设您的应用程序根目录(与app.yaml相邻)中有一个名为data的文件夹,其中包含一个名为list.txt的文件。您可以像这样读取其内容:

if content, err := ioutil.Readfile("data/list.txt"); err != nil {
    // 读取文件失败,处理错误
} else {
    // 读取成功,对内容进行操作
}

或者,如果您需要一个io.Readeros.File实现了io.Reader以及许多其他功能):

f, err := os.Open("data/list.txt") // 用于读取访问权限。
if err != nil {
    // 打开文件失败,记录/处理错误
    return
}
defer f.Close()
// 在这里您可以从f中读取

相关问题:

https://stackoverflow.com/questions/29285670/google-app-engine-golang-no-such-file-or-directory

https://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine

https://stackoverflow.com/questions/34629056/how-do-i-store-the-private-key-of-my-server-in-google-app-engine

英文:

You can read from files on App Engine the same way you can read from files in a Go app running on your computer.

Some things to keep in mind:

  • You should use relative file paths instead of absolute. The working directory is the root folder of your app (where the app.yaml file resides).

  • Only files that are application files can be read by Go code, so if you want to read a file from Go code, the file must not be matched by a static file pattern (or if it must be available as a static file too, application_readable option must be specified at the static file handler which includes/applies to the file, details).

The latter 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.

So let's say you have a folder data in your app's root (next to app.yaml), and a file list.txt in it. You may read its content like this:

if content, err := ioutil.Readfile("data/list.txt"); err != nil {
    // Failed to read file, handle error
} else {
    // Success, do something with content
}

Or if you want / need an io.Reader (os.File implements io.Reader along with many other):

f, err := os.Open("data/list.txt") // For read access.
if err != nil {
	// Failed to open file, log / handle error
    return
}
defer f.Close()
// Here you may read from f

Related questions:

https://stackoverflow.com/questions/29285670/google-app-engine-golang-no-such-file-or-directory

https://stackoverflow.com/questions/35638518/static-pages-return-404-in-google-app-engine

https://stackoverflow.com/questions/34629056/how-do-i-store-the-private-key-of-my-server-in-google-app-engine

huangapple
  • 本文由 发表于 2016年4月19日 19:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/36717098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定