2017-09-30 5 views
0

Je développe mon application sous Windows avec GAE, mais le message d'erreur ci-dessous s'affiche.ExtractionError: impossible d'extraire le (s) fichier (s) en cache

C:\Python27\Lib\site-packages 

Perhaps your account does not have write access to this directory? You can 

change the cache directory by setting the PYTHON_EGG_CACHE environment 

variable to point to an accessible directory. 

Je vérifié un certain poste dans ce site et je trouve some articles, mais je ne pouvais pas le comprendre. Ma compréhension est que j'ai besoin de créer un répertoire de cache avec un accès en écriture dans le répertoire de base, mais je n'ai pas compris où se trouve "home directory".

Également dans un autre post, j'ai trouvé la réponse en disant Cette approche a résolu mon problème. J'ai désinstallé pyyaml ​​en utilisant pip, puis l'ai installé avec easy_install -z pyyaml ​​

Quelle approche est correcte?

import webapp2 
import os 
import jinja2 
import cloudstorage 
import mimetypes 

from PIL import Image 
from google.appengine.ext import ndb 
from google.appengine.ext import blobstore 
from google.appengine.api import users 
from google.appengine.api import app_identity 
from google.appengine.api import images 
from models import Note 
from models import CheckListItem 
from models import NoteFile 

jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) 

images_formats = { 
'0':'image/png', 
'1':'image/jpeg', 
'2':'image/webp', 
'-1':'image/bmp', 
'-2':'image/gif', 
'-3':'image/ico', 
'-4':'image/tiff', 
} 

class MainHandler(webapp2.RequestHandler): 
def get(self): 
    user = users.get_current_user() 
    if user is not None: 
     logout_url = users.create_logout_url(self.request.uri) 
     template_context = { 
      'user': user.nickname(), 
      'logout_url' : logout_url, 
     } 
     template = jinja_env.get_template('main.html') 
     self.response.out.write(template.render(template_context)) 
    else: 
     login_url = users.create_login_url(self.request.uri) 
     self.redirect(login_url) 

def post(self): 
    user = users.get_current_user() 
    if user is None: 
     self.error(401) 

    bucket_name = app_identity.get_default_gcs_bucket_name() 
    uploaded_file = self.request.POST.get('uploaded_file') 
    file_name = getattr(uploaded_file,'filename',None) 
    file_content = getattr(uploaded_file,'file',None) 
    real_path = '' 
    if file_name and file_content: 
     content_t = mimetypes.guess_type(file_name)[0] 
     real_path = '/' + bucket_name + '/' + user.user_id() + "/" + file_name 
     with cloudstorage.open(real_path,'w',content_type=content_t,options={'x-goog-acl':'public-read'}) as f: 
      f.write(file_content.read()) 

    self._create_note(user, file_name, real_path) 

    logout_url = users.create_logout_url(self.request.uri) 
    template_context = { 
     'user':user.nickname(), 
     'logout_url': logout_url, 
    } 
    self.response.out.write(self._render_template('main.html',template_context)) 

def _render_template(self,template_name,context=None): 
    if context is None: 
     context = {} 
    user = users.get_current_user() 
    ancestor_key = ndb.Key("User",user.nickname()) 
    qry = Note.owner_query(ancestor_key) 
    context['notes'] = qry.fetch() 
    template = jinja_env.get_template(template_name) 
    return template.render(context) 


@ndb.transactional 
def _create_note(self,user,file_name,file_path): 
    note = Note(parent=ndb.Key("User", user.nickname()), title=self.request.get('title'), content=self.request.get('content')) 
    note.put() 
    item_titles = self.request.get('checklist_items').split(',') 
    for item_title in item_titles: 
     item = CheckListItem(parent=note.key, title=item_title) 
     item.put() 
     note.checklist_items.append(item.key) 
    if file_name and file_path: 
     url, thumbnail_url = self._get_urls_for(file_name) 

     f = NoteFile(parent=note.key, name=file_name, url=url,thumbnail_url=thumbnail_url,full_path=file_path) 
     f.put() 

     note.files.append(f.key) 
     note.put() 

def _get_urls_for(self,file_name): 
    user = users.get_current_user() 
    if user is None: 
     return 
    bucket_name = app_identity.get_default_gcs_bucket_name() 
    path = '/' + bucket_name + '/' + user.user_id() + '/' + file_name 
    real_path = '/gs' + path 
    key = blobstore.create_gs_key(real_path) 
    try: 
     url = images.get_serving_url(key, size=0) 
     thumbnail_url = images.get_serving_url(key,size=150,crop=True) 
    except images.TransformationError,images.NotImageError: 
     url = "http://storage.googleapis.com{}".format(path) 
     thumbnail_url = None 

    return url,thumbnail_url 



class MediaHandler(webapp2.RequestHandler): 
def get(self,file_name): 
    user = users.get_current_user() 
    bucket_name = app_identity.get_default_gcs_bucket_name() 
    content_t = mimetypes.guess_type(file_name)[0] 
    real_path = '/' + bucket_name + '/' + user.user_id() + '/' + file_name 
    try: 
     with cloudstorage.open(real_path,'r')as f: 
      self.response.headers.add_header('Content-Type',content_t) 
      self.response.out.write(f.read()) 
    except cloudstorage.errors.NotFoundError: 
     self.abort(404) 

class ShrinkHandler(webapp2.RequestHandler): 
def _shrink_note(self,note): 
    for file_key in note.files: 
     file = file_key.get() # this is the same as "file.get().url" in html file. we add the comment. 
     try: 
      with cloudstorage.open(file.full_path) as f: 
       image = images.Image(f.read()) 
       image.resize(640) 
       new_image_data = image.execute_transforms() 

      content_t = images_format.get(str(image.format)) 
      with cloudstorage.open(file.full_path,'w',content_type=content_t) as f: 
       f.write(new_image_data) 
     except images.NotImageError: 
      pass 

def get(self): 
    user = users.get_current_user() 
    if user is None: 
     login_url = users.create_login_url(self.request.url) 
     return self.redirect(login_url) 

    ancestor_key = ndb.Key("User",user.nickname()) 
    notes = Note.owner_query(ancestor_key).fetch() 

    for note in notes: 
     self._shrink_note(note) 

    self.response.write('Done.') 

app = webapp2.WSGIApplication([ 
(r'/', MainHandler), 
(r'/media/(?P<file_name>[\w.]{0,256})',MediaHandler), 
(r'/shrink',ShrinkHandler) 
], debug=True) 

Répondre

1

On dirait que vous développez une application norme GAE env mais vous essayez d'installer des bibliothèques de tiers dans (et de les utiliser de) votre forfaits de site du système local. Cela ne marchera pas.

Vous devez installer les bibliothèques de tiers dans le répertoire bibliothèque de votre application (par exemple en utilisant l'option -t <your_lib_dir> pour pip install, pour lesquelles vous ne devriez pas besoin de permissions spéciales Voir aussi:..