2011-12-14 89 views
4

我是Google新應用引擎的新成員,我希望將其用作服務器以便人們下載文件。我已經閱讀了python中的教程。我沒有找到任何實際指導我如何上傳文件到服務器的目的。上傳Google App Engine中的文件並將其下載

+0

該文檔提供了一個預先寫好的樣本,完全可以做到這一點。你看起來在哪裏? – 2011-12-14 03:24:11

回答

4

Blobstore tutorial給出了這個用例的一個例子。這鏈接提供了此代碼:一個應用程序,允許用戶上傳文件,然後立即將它們下載:

#!/usr/bin/env python 
# 

import os 
import urllib 

from google.appengine.ext import blobstore 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import blobstore_handlers 
from google.appengine.ext.webapp import template 
from google.appengine.ext.webapp.util import run_wsgi_app 

class MainHandler(webapp.RequestHandler): 
    def get(self): 
     upload_url = blobstore.create_upload_url('/upload') 
     self.response.out.write('<html><body>') 
     self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url) 
     self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" 
      name="submit" value="Submit"> </form></body></html>""") 

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): 
    def post(self): 
     upload_files = self.get_uploads('file') # 'file' is file upload field in the form 
     blob_info = upload_files[0] 
     self.redirect('/serve/%s' % blob_info.key()) 

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
    def get(self, resource): 
     resource = str(urllib.unquote(resource)) 
     blob_info = blobstore.BlobInfo.get(resource) 
     self.send_blob(blob_info) 

def main(): 
    application = webapp.WSGIApplication(
      [('/', MainHandler), 
      ('/upload', UploadHandler), 
      ('/serve/([^/]+)?', ServeHandler), 
      ], debug=True) 
    run_wsgi_app(application) 

if __name__ == '__main__': 
    main() 
0

您還可以檢查從尼克·約翰遜的博客,有一個很好的接口very good GAE/python app並且還能夠在你需要多個上傳。我已經將這些代碼用於構建需要類似文件系統和管理blob的應用程序。

相關問題