2012-07-24 56 views
0

我是google appengine的新手。 我想上传一个文件到一个目录而不是存储为blob。想使用python上传文件到google appengine中的目录

main.py

import cgi 
import webapp2 
from google.appengine.api import users 
from google.appengine.ext.webapp.template \ 
    import render 
from os import path 

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     context={} 
     tmpl = path.join(path.dirname(__file__), 'static/html/index.html') 
     self.response.out.write(render(tmpl, context)) 

    def post(self): 
     form_data = self.request.get('file') 
     file_data = form_data 
     f=open('static/html/'+form_data,'w') 
     f.write(file_data) 
     f.close 



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

的index.html

> <html> 
>  <head><title>test</title></head> 
>  <body>hello 
>  <form id="addmovieform" action="/" method="post" ENCTYPE="multipart/form-data"> 
>   <input type="file" name="file" > 
>   <input type="submit" name="submit"> 
>  </form> 
>  </body> 
>   </html> 

错误

Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in call rv = self.handle_exception(request, response, e) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in call rv = self.router.dispatch(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher return route.handler_adapter(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in call return handler.dispatch() File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch return self.handle_exception(e, self.app.debug) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch return method(*args, **kwargs) File "/Users/saravase/test/main.py", line 33, in post f=open('static/html/'+form_data,'w') File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 589, in init raise IOError('invalid mode: %s' % mode) IOError: invalid mode: w

请指导我...

+0

你为什么要这样做?在文件系统中存储数据绝对没有什么特别之处 - 实际上你可以通过编写一个你可以用'StringIO'或blobstore完成的文件来做任何事情。 – 2012-07-27 05:43:57

回答

3

从 “What is Google App Engine?

Applications cannot write to the file system in any of the runtime environments. An application can read files, but only files uploaded with the application code. The app must use the App Engine datastore, memcache or other services for all data that persists between requests. The Python 2.7 environment allows bytecode to be read, written, and modified.

根据应用程序的需要,您需要返回使用blobstore或尝试Google Cloud Storage API

+0

谢谢..你的回复.. – Saravanan 2012-07-25 13:32:11

相关问题