How to list the files in a static directory?

2019-04-06 14:45发布

I am playing with Google App Engine and Python and I cannot list the files of a static directory. Below is the code I currently use.

app.yaml

- url: /data
  static_dir: data

Python code to list the files

myFiles = []
for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), 'data/') ):
    for name in files:
        full_name = os.path.join(root, name)
        myFiles.append('%s;%s\n' % (name, datetime.fromtimestamp(os.stat(full_name).st_mtime)))

When I run this code locally on my machine, everything is alright. I have my Python script at the root of the directory and it walks the files under the data directory. However, when I upload and run the exact same code in GAE, it doesn`t work. It seems to me that the directory structure of my application is not exactly replicated in Google App Engine. Where are the static files?

Thanks!

3条回答
对你真心纯属浪费
2楼-- · 2019-04-06 15:07

https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Static_file_handlers

They're not where you think they are, GAE puts static content into GoogleFS which is equivalent of a CDN. The idea is that static content is meant to be served directly to your users and not act as a file store you can manipulate. Furthermore GAE has 1K file limit and it would be difficult to police this rule if you could manipulate your static file store.

查看更多
3楼-- · 2019-04-06 15:15

Here´s a project that let you browse your static files: http://code.google.com/p/appfilesbrowser/

And here is must see list of recipes for appengine: http://appengine-cookbook.appspot.com/ (I found about that project here sometime ago)

查看更多
一夜七次
4楼-- · 2019-04-06 15:24

You can't access files uploaded as static content programmatically - they're not installed on the server along with your app, rather they're served up directly. If you really need to access them, you can remove the static file handler and serve them up yourself.

查看更多
登录 后发表回答