1
class PDF(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = "application/txt"
        self.response.headers['Content-Disposition'] = "attachment; filename=file.pdf"
        f = open('/Users/BP/file.pdf', 'r')
        self.response.out.write(f.read())
def main():
    application = webapp.WSGIApplication([('/download', PDF)],
                                        debug=False)
    util.run_wsgi_app(application)

I get this error when I try to download it:

[Errno 13] Permission denied: '/Users/BP/file.pdf'
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/s~projectname/1.354763951774324185/main.py", line 43, in get
    f = open('/Users/BP/file.pdf', 'r')
IOError: [Errno 13] Permission denied: '/Users/BP/file.pdf'

Even though I've tried the chmod a+r file.pdf Help please. Thank you!

BPm
  • 2,924
  • 11
  • 33
  • 51

4 Answers4

3

os.path.dirname(__file__) gives you the application directory.

f = open(os.path.dirname(__file__) + /'BP/file.pdf', r)

Store your file in BP/ folder inside the main folder.

@Nicke, @Jon: GAE allows access to your files. "May the source be with you".

Alvin K.
  • 4,329
  • 20
  • 25
  • The "correct" way in GAE is to use static file handle in app.yaml, if you enjoy DIY style, try this [Webob](http://docs.webob.org/en/latest/file-example.html) example which is more efficient. – Alvin K. Nov 20 '11 at 21:15
  • I saw that some framework do this and I'm not sure it's recommended that we use the `os`module at all when doing WSGI. Of course since "everything is a file" in UNIX then naturally you're right saying I was wrong here. And well, if you want to use to os module Iäm not sure how forward-comaptible with WSGi that is. If you are programming WSGI I'm not sure that it's recommended to use the `os` module. – Niklas Rosencrantz Nov 22 '11 at 01:35
  • @AlvinK. if you need to serve a file from a requesthandler, for instance when getting a REST request and in the requesthandler you evaluate a condition, if true you should send file, if false you should send a json string. This is not possible doing with a static file handle in app.yaml right? – Peter Warbo Nov 22 '12 at 14:26
  • @PeterWarbo: I'd use [custom error response](https://developers.google.com/appengine/docs/python/config/appconfig#Custom_Error_Responses) for handling errors. Else you can try the [catch all errors](http://stackoverflow.com/questions/189751/google-app-engine-and-404-error) from SO. – Alvin K. Nov 28 '12 at 18:34
2

GAE doesn't have access to files. If you want to serve a file, serve it from a static directory or from the blobstore.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
2

The user running AppEngine does not have read access to the directory. Try to chmod the BP dir.

But even so this will not work once you deploy your app. There is no notion of a file system in App Engine. May I suggest you store the file in a blob or in the data store. Or static directory.

Jon Nylander
  • 8,743
  • 5
  • 34
  • 45
  • 1
    "There is no notion of a file system on App Engine" - this is completely untrue. The filesystem is merely read-only. – Nick Johnson Nov 21 '11 at 04:49
  • 1
    You're right of course. What I am aiming to say is that you cannot put things in a random folder on your system, chmod it, and expect it to work once deployed. – Jon Nylander Nov 21 '11 at 09:17
  • Interesting comments guys. What is a file? "In UNIX everything is a file" and I don't agree with that statement. – Niklas Rosencrantz Nov 22 '11 at 01:33
  • We look to the (Linux creator) [Linus Torvards](http://yarchive.net/comp/linux/everything_is_file.html). His own words: The UNIX philosophy is often quoted as "everything is a file", but that really means "everything is a stream of bytes". – Alvin K. Nov 22 '11 at 05:57
0

Can also serve static files, don't need a handler for this

- url: /sampleData\.csv
  static_files: sampleData.txt
  upload: sampleData\.csv
  http_headers:
    Content-Type: text/csv
    Content-Disposition: "attachment; filename=sample.csv"
sandeep koduri
  • 500
  • 2
  • 15