Google app engine 使用appengine提供静态文件
我创建了一个应用程序引擎应用程序。到目前为止,我只有几个HTML文件可供使用。当有人访问时,我可以做些什么使App Engine为index.html文件提供服务 当前,我的app.yaml文件如下所示:Google app engine 使用appengine提供静态文件,google-app-engine,static-html,Google App Engine,Static Html,我创建了一个应用程序引擎应用程序。到目前为止,我只有几个HTML文件可供使用。当有人访问时,我可以做些什么使App Engine为index.html文件提供服务 当前,我的app.yaml文件如下所示: application: appname version: 1 runtime: python api_version: 1 handlers: - url: / static_dir: static_files 在WEB-INF/WEB.xml中,输入: <welcome
application: appname
version: 1
runtime: python
api_version: 1
handlers:
- url: /
static_dir: static_files
在WEB-INF/WEB.xml中,输入:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
index.html
如果您试图将/
映射到index.html
:
handlers:
- url: /
upload: folderpath/index.html
static_files: folderpath/index.html
application: your-app-name-here
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.css)
mime_type: text/css
static_files: static/\1
upload: static/(.*\.css)
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
- url: /(.*\.js)
mime_type: text/javascript
static_files: static/\1
upload: static/(.*\.js)
- url: /(.*\.txt)
mime_type: text/plain
static_files: static/\1
upload: static/(.*\.txt)
- url: /(.*\.xml)
mime_type: application/xml
static_files: static/\1
upload: static/(.*\.xml)
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: static/\1
upload: static/(.*\.(bmp|gif|ico|jpeg|jpg|png))
# index files
- url: /(.+)/
static_files: static/\1/index.html
upload: static/(.+)/index.html
# redirect to 'url + /index.html' url.
- url: /(.+)
static_files: static/redirector.html
upload: static/redirector.html
# site root
- url: /
static_files: static/index.html
upload: static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script language="JavaScript">
self.location=self.location + "/";
</script>
</head>
<body>
</body>
</html>
url:
将在路径上匹配并支持regex
- url: /images
static_dir: static_files/images
因此,如果您的图像文件存储在static\u files/images/picture.jpg
中,请使用以下命令:
<img src="/images/picture.jpg" />
这应该可以满足您的需要: 说明:在应用程序引擎Python中,可以使用正则表达式作为
App.yaml
中的URL处理程序,并将所有URL重定向到静态文件的层次结构
示例app.yaml
:
handlers:
- url: /
upload: folderpath/index.html
static_files: folderpath/index.html
application: your-app-name-here
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.css)
mime_type: text/css
static_files: static/\1
upload: static/(.*\.css)
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
- url: /(.*\.js)
mime_type: text/javascript
static_files: static/\1
upload: static/(.*\.js)
- url: /(.*\.txt)
mime_type: text/plain
static_files: static/\1
upload: static/(.*\.txt)
- url: /(.*\.xml)
mime_type: application/xml
static_files: static/\1
upload: static/(.*\.xml)
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: static/\1
upload: static/(.*\.(bmp|gif|ico|jpeg|jpg|png))
# index files
- url: /(.+)/
static_files: static/\1/index.html
upload: static/(.+)/index.html
# redirect to 'url + /index.html' url.
- url: /(.+)
static_files: static/redirector.html
upload: static/redirector.html
# site root
- url: /
static_files: static/index.html
upload: static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script language="JavaScript">
self.location=self.location + "/";
</script>
</head>
<body>
</body>
</html>
为了处理对URL的请求,而这些请求的结尾不是可识别的类型(.html
,.png
,等等)或/
,您需要将这些请求重定向到URL+/
,以便为该目录提供index.html
。我不知道在app.yaml
中有什么方法可以做到这一点,所以我添加了一个javascript重定向器。这也可以通过一个小型python处理程序来完成
redirector.html
:
handlers:
- url: /
upload: folderpath/index.html
static_files: folderpath/index.html
application: your-app-name-here
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.css)
mime_type: text/css
static_files: static/\1
upload: static/(.*\.css)
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
- url: /(.*\.js)
mime_type: text/javascript
static_files: static/\1
upload: static/(.*\.js)
- url: /(.*\.txt)
mime_type: text/plain
static_files: static/\1
upload: static/(.*\.txt)
- url: /(.*\.xml)
mime_type: application/xml
static_files: static/\1
upload: static/(.*\.xml)
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: static/\1
upload: static/(.*\.(bmp|gif|ico|jpeg|jpg|png))
# index files
- url: /(.+)/
static_files: static/\1/index.html
upload: static/(.+)/index.html
# redirect to 'url + /index.html' url.
- url: /(.+)
static_files: static/redirector.html
upload: static/redirector.html
# site root
- url: /
static_files: static/index.html
upload: static/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script language="JavaScript">
self.location=self.location + "/";
</script>
</head>
<body>
</body>
</html>
self.location=self.location+“/”;
可以使用(app.yaml)完成:
下面是app.yaml,介绍我是如何让Jekyll生成的站点工作的:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: _site/index.html
upload: _site/index.html
- url: /assets
static_dir: _site/assets
# index files
- url: /(.+)/
static_files: _site/\1/index.html
upload: _site/(.+)/index.html
- url: /(.*)
static_files: _site/\1
upload: _site/(.*)
- url: /.*
static_dir: _site
如果
index.html
位于您的根目录中,请忽略folderpath/
。现在我可以访问index.html,但无法访问存储在静态文件/图像中的图像。请小心,处理程序的顺序很重要!谢谢你的评论@MrPhil。你为我节省了一些时间;)能在这里描述一下你的解决方案吗?如果链接消失,你的回答也会消失虽然这可能对OP有效,但这里有两个正确、非常简洁和准确的答案,与之相比这是不正确的。这不是应该怎么做的。很好地解释了我在这种方法中遇到的一个问题,并发布了一个关于堆栈溢出的问题。对静态文件的请求击中了我的nodejs服务器(这意味着它们没有使用CDN)。他使用的是Python而不是Java。这是一个Java示例,如果能完整地解释一下就好了。