Google App Engine ではプログラムから static ファイルを読みこめない
Google App Engine で、スクリプトから画像ファイルの一覧を取り出そうとしたけど、できなくてはまった。
helloworld.py
# -*- coding: utf-8 -*- import os, glob print "Content-Type: text/plain" print "" print "hello" imgdir = os.path.join(os.path.dirname(__file__), 'img') for x in glob.glob(imgdir + '/*'): print x #=> 何も表示されない print open(imgdir + '/favicon.ico') #=> IOError: [Errno 13] file not accessible
ドキュメントによれば、ファイルへの書き込みはできなくても読み込みはできるはずなんだけど。
アプリケーションは、ファイル システムへの書き込みはできません。ファイルの読み取りはできますが、アプリケーション コードとともにアップロードされたファイルのみに限られます。
http://code.google.com/intl/ja/appengine/docs/whatisgoogleappengine.html
と思ってたら、こんな投稿が。
You can read files that weren't marked as static resources, in the regular fashion.
Google グループ
どうも、static ファイルとして設定したファイルは、スクリプトから読み出せないようだ。
application: helloworld
version: 1
runtime: python
api_version: 1
handlers:
- url: /css
static_dir: css
- url: /js
static_dir: js
- url: /img
static_dir: img # 'img' 以下は static ファイルとして指定
- url: /.*
script: helloworld.pyつまり、static ファイルとそうでないファイルとでは、そもそもファイルの配置場所が違うんだろうな。だからスクリプトからは static ファイルが簡単には読み出せないんだろう。
ところでこの制限は公式のドキュメントに書かれてあるんだろうか。ご存知の方がいましたら教えてください。