开发者

Read a file on App Engine with Python?

开发者 https://www.devze.com 2022-12-26 11:47 出处:网络
Is it possible to open a file on GAE just to read its contents and get the last modified tag? I get a IOError: [Errno 13] file not accessible:

Is it possible to open a file on GAE just to read its contents and get the last modified tag?

I get a IOError: [Errno 13] file not accessible: I know that i cannot delete or update but i believe reading should be possible Has anyone faced a similar problem?

os.sta开发者_如何学Pythont(f,'r').st_mtim


You've probably declared the file as static in app.yaml. Static files are not available to your application; if you need to serve them both as static files and read them as application files, you'll need to include 2 copies in your project (ideally using symlinks, so you don't actually have to maintain an actual copy.)

Update Nov 2014:

As suggested in the comments, you can now do this with the application_readable flag:

application_readable

Optional. By default, files declared in static file handlers are uploaded as static data and are only served to end users, they cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.

See https://cloud.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers


You can read files, but they're on Goooogle's wacky GAE filesystem so you have to use a relative path. I just whipped up a quick app with a main.py file and test.txt in the same folder. Don't forget the 'e' on st_mtime.

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class MainHandler(webapp.RequestHandler):

  def get(self):
    path = os.path.join(os.path.split(__file__)[0], 'test.txt')

    self.response.out.write(os.stat(path).st_mtime)


def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()


+1 for the new "application_readable: true" feature. Before using this new feature I did run into an issue with GAEs' "wacky" file system while getting the NLP Montylingua to import.

Issue: Monty uses the open(filename,'rb') and a file pointer to file_ptr.read() in bytes from the static files. My implementation worked on my local windows system but failed upon deployment!

The fix: Specify the expected bytes to read file_ptr.read(4) #4 binary bytes

Appears to be something related to the 64 bit GAE server wanting to read in more (8 by default) bytes. Anyways, took a while to find that issue. Montylingua loads now.


I came up strange but working solution :) Jinja :)

Serving static files directly sometimes become a headache with GAE. Possible trade-off from performance let you move straigh forward with Jinja

- url: /posts/(.*\.(md|mdown|markdown)) 
  mime_type: text/plain
  static_files: static/posts/\1
  upload: posts/(.*\.(md|mdown|markdown))



from jinja2 import Environment
from jinja2.loaders import FileSystemLoader
posts = Environment(loader=FileSystemLoader('static/posts/')) # Note that we use static_files folder defined in app.yaml
post = posts.get_template('2013-11-13.markdown')

import markdown2 # Does not need of course

class Main(webapp2.RequestHandler):

    def get ( self ):
        self.response.headers[ 'Content-Type' ] = 'text/html'

        self.response.write ( markdown2.markdown( post.render()) )  # Jinja + Markdown Render function

Did you get it ;) I tested and It worked.


With webapp2, supposing you have pages/index.html at the same path as main.py:

#!/usr/bin/env python

import webapp2, os

class MainHandler(webapp2.RequestHandler):
    def get(self):
        path = os.path.join(os.path.split(__file__)[0], 'pages/index.html')
        with open(path, 'r') as f:
            page_content = f.read()
        self.response.write(page_content)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)


I can't see an answer for when the file hasn't been marked as static, and you're trying to read it in mode 'rt'; apparently that doesn't work. You can however open files just fine in mode 'rb', or just plain 'r'. (I wasted about 10 minutes on that 't'.)

0

精彩评论

暂无评论...
验证码 换一张
取 消