在AppEngine中加载文件
我有一小段代码用来显示一个文件
在 app.yaml 文件里
- url: /(.*\.(gif|png|jpg))
static_files: static/\1
upload: static/(.*\.(gif|png|jpg))
在 main.py 文件里
...
class ShowImage(webapp.RequestHandler):
def get(self):
rootpath = os.path.dirname(__file__)
file = rootpath + "/static/tracker.gif";
fh=open(file, 'r')
self.response.out.write(fh.read())
fh.close
...
我可以通过访问我的 *.appspot.com/tracker.gif 来查看文件是否上传成功(根据 app.yaml 的设置)。但是如果我使用 *.appspot.com/showimage,就会返回
Traceback (most recent call last):
File "/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 510, in __call__
handler.get(*groups)
File "/base/data/home/apps/APPNAME/2.341131266814384624/main.py", line 170, in get
fh=open(file, 'r')
IOError: [Errno 2] No such file or directory: '/base/data/home/apps/APPNAME/2.341131266814384624/static/tracker.gif'
2 个回答
1
为了让大家更明白Chris M.说的是什么:
在你发布应用程序的时候,任何符合“上传”属性的文件,都会被放到一个和你的代码及相关文件完全不同的地方。对你的代码来说,这些文件就像是从你原本预期的位置消失了一样。
3
已移除
- url: /(.*\.(gif|png|jpg))
static_files: static/\1
upload: static/(.*\.(gif|png|jpg))
从 app.yaml 文件中,显然你不能从那些你愚蠢地标记为静态的文件夹中提供内容。
关于这个问题,可以参考 将静态目录内容部署到谷歌应用引擎 的讨论。