0
 new_file = open('../pages/pdf/test.PDF', 'rb').read()

keeps giving me file/directory not found

I also tried this in my app.yaml :

 - url: /test.((pdf)|(PDF))
   static_files: pages/pdf/test.PDF
   upload: pages/pdf/test.PDF

File structure

 -app.yaml
 pages
   -index.html
   -page1.html
   pdf
     -test.PDF
 entities
   -booking.py (file that im writing the above code)

how to properly open a file in google app engine?

Here are the app.yaml handlers

 - url: /sitemap.xml
   script: sitemap.app

 - url: /test.((pdf)|(PDF))
   static_files: pages/pdf/test.PDF
   upload: pages/pdf/test.PDF
   application_readable: true

 - url: /images
   static_dir: pages/images

 - url: /fonts
   static_dir: pages/fonts

 - url: /css
   static_dir: pages/css
   expiration: 1s

Trying to attach the above file as below using EmailMessage :

 new_file = open(os.path.dirname(__file__) +'/../pages/pdf/test.PDF').read()

 message = mail.EmailMessage( sender=EMAIL_SENDER, 
 subject=subject,body=theBody,to=['myemail@gmail.com'],attachments= 
 [('test',new_file)])

 message.send()

In debuggin it says : Invalid attachment type

codec
  • 47
  • 1
  • 7

1 Answers1

0

By default files included in your app's static content (i.e. matching a static_files or static_dir handler configuration pattern) aren't accessible to your application code. If you want such access you need to set the application_readable: true option for the respective app.yaml handler configurations. See GAE: file_get_contents() and static files

Once you do that the file will be accessible using a path relative to your application/service root directory (i.e. where the app.yaml file is located), in your case it'll be pages/pdf/test.PDF.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • thxxx! @Dan Cornilescu open('pages/pdf/test.PDF', 'rb').read(), but I still get invalid file for some reason even after adding application_readable: true – codec Jun 25 '18 at 18:25
  • can you pls update the post with all the static handler configs deployed? – Dan Cornilescu Jun 25 '18 at 18:39
  • Added app.yaml handlers. btw shouldn't it be ('../pages/pdf/test.PDF','rb') instead of ('pages/pdf/test.PDF','rb'). as the current file is inside entities? – codec Jun 25 '18 at 18:48
  • Hm, it *should* work :) The only other thing that crosses my mind would be case (in)sensitivity in the file name/path. Check your actually deployed files, see https://stackoverflow.com/questions/47498821/google-cloud-datastore-automatic-indexing/47499884#47499884 – Dan Cornilescu Jun 25 '18 at 18:54
  • Files *should* be accessible from the app's root dir, without the `../` in your case - at least that's how imports work. But give the `../` a try as well, just in case I'm wrong. – Dan Cornilescu Jun 25 '18 at 18:59
  • BTW - is the same working with your development server? – Dan Cornilescu Jun 25 '18 at 19:00
  • this is actually in the development server :D and we are stuck...it seems like the file is properly being linked with the ../ . thanks a lot!! but the final step of us trying to link this file is to send it as an attachment. It keeps saying invalid attachment > – codec Jun 25 '18 at 19:19
  • Is the file actually a (sym)link? That could be the issue on the development server (depending on how exactly the file is symlinked and used). When deployed the symlinks are replaced with actual copies of the files they're pointing to, so such issues would not be seen on GAE. – Dan Cornilescu Jun 25 '18 at 19:23
  • I not am sure about sym link, I have added the EmailMessage where I am trying to add as an attachment – codec Jun 25 '18 at 19:34
  • Don't mix the email attachment with this question, that's most likely a different issue - I see you already have a separate question for that. You can check that your file is readable with a temporary `logging.info(new_file)` statement. – Dan Cornilescu Jun 25 '18 at 19:54