英文:
Static files not serving when uploaded in Google app engine (SDK Go)
问题
我正在尝试使用Google App Engine云空间。我的程序在本地主机上运行没有任何问题,但是当我尝试在Google App Engine上托管时,静态文件无法提供服务。app.yaml文件如下所示:
application: myTestApp-nn34322
version: 1
runtime: go
threadsafe: true
api_version: go1
handlers:
-
url: /static/css
static_dir: ../static/css
mime_type: "text/css" -
url: /static/js
static_dir: ../static/js
mime_type: "text/javascript" -
url: /static/images
static_dir: ../static/images -
url: /.*
script: _go_app
英文:
I was trying to use google app engine cloud space. My program is running without any problem in localhost, but when I tries to host it in google app engine, static files are not served. The app.yaml is as follows:
application: myTestApp-nn34322
version: 1
runtime: go
threadsafe: true
api_version: go1
handlers:
- url: /static/css
static_dir: ../static/css
mime_type: "text/css"
- url: /static/js
static_dir: ../static/js
mime_type: "text/javascript"
- url: /static/images
static_dir: ../static/images
- url: /.*
script: _go_app
答案1
得分: 1
你应该将所有静态资源放在与app.yaml
文件相同的文件夹下,否则它们将无法上传到AppEngine。
所以你应该将你的static
文件夹放在app.yaml
旁边,然后正确的路径就是static/xxx
,例如:
- url: /static/css
static_dir: static/css
mime_type: "text/css"
- url: /static/js
static_dir: static/js
mime_type: "text/javascript"
- url: /static/images
static_dir: static/images
注意:
如果你打算在Go应用程序中使用这些静态文件(例如,你想读取它们的内容),那就需要特殊处理,因为与静态文件模式匹配的资源在上传后不会被复制到应用程序中(静态文件由单独的服务器提供)。有关详细信息,请参阅https://stackoverflow.com/questions/29285670/google-app-engine-golang-no-such-file-or-directory/29288854#29288854。基本上,你要么必须复制静态文件,要么为静态文件处理程序提供application_readable
选项。
英文:
You should put every static resources under the same folder where you put your app.yaml
file, else they won't get uploaded to AppEngine.
So you should put your static
folder next to app.yaml
, and then of course the correct path is simply static/xxx
, e.g.:
- url: /static/css
static_dir: static/css
mime_type: "text/css"
- url: /static/js
static_dir: static/js
mime_type: "text/javascript"
- url: /static/images
static_dir: static/images
Note:
If you intend to use these static files from your Go app too (e.g. you want to read their content), that requires special handling because resources matched by static file patterns will not be copied to the application after uploading (static files are served by separate servers). For details, see https://stackoverflow.com/questions/29285670/google-app-engine-golang-no-such-file-or-directory/29288854#29288854. Basically you either have to duplicate the static files, or provide the application_readable
option to the static file handler.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论