2012-03-26 57 views
-1

我工作的小項目(即成)文件得到的Blob存儲在GAE

2-將這些上傳的文件壓縮到單個zip壓縮文件中。

將此Zip文件存儲在GAE BlobStore中。

將此zip從blobstore獲取(服務)到PC中(下載此zip以在本地獲得)。

步驟1,2和3正確完成,但步驟4中的問題;我不能從blobstore下載此Zip; 這是我使用的代碼:

from __future__ import with_statement 
from google.appengine.api import files 
import cgi, cgitb ; cgitb.enable() 
import StringIO 
import zipfile 
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.util import run_wsgi_app 
glob_blob_info="" 
class zip(): 
    def z(self): 
     form = cgi.FieldStorage() 
     zipstream=StringIO.StringIO() 
     zfile = zipfile.ZipFile(file=zipstream,mode="w",compression=zipfile.ZIP_DEFLATED) 
     file_upload = form['file[]'] 
     filename2 = file_upload.filename 
     data=file_upload.file.read() 
     zfile.writestr(filename2,data) 
     zfile.close() 
     zipstream.seek(0) 
     zip_file = files.blobstore.create(mime_type='application/zip',_blobinfo_uploaded_filename='test.zip') 
     with files.open(zip_file, 'a') as f: 
      f.write(zipstream.getvalue()) 
     files.finalize(zip_file) 
     blob_key = files.blobstore.get_blob_key(zip_file) 
     print blob_key 
     blob_info = blobstore.BlobInfo.get(blob_key) 
     print blob_info 
     global glob_blob_info 
     glob_blob_info=blob_info 
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
    def get(self): 
    print "doaa" 
    global glob_blob_info 
    glob_blob_info = urllib.unquote(glob_blob_info) 
    blob_info = blobstore.BlobInfo.get(glob_blob_info) 
    self.send_blob(glob_blob_info,save_as=True) 



def main(): 
    application = webapp.WSGIApplication([('/serve', ServeHandler),], debug=True) 
    debug=True) 
    c=zip() 
    c.z() 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 

現在class zip()執行successfuly使得含有由用戶上載的文件zip存檔在GAE Blob存儲successfuly創建的,但問題正是該class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):沒」牛逼執行 當我跑這個代碼,我有這樣的輸出:

Status: 404 Not Found 
Content-Type: text/html; 
charset=utf-8 Cache-Control: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Content-Length: 0 

有任何想法,這個問題在此先感謝?

+0

請停止發佈幾乎相同的問題。 – 2012-03-27 11:14:56

+1

[SendEblob in GAE]的可能重複(http://stackoverflow.com/questions/9880865/send-blob-in-gae) – 2012-03-27 11:15:17

+0

@Nick Johnson:對不起,但問題仍然存在! – 2012-03-27 16:21:11

回答

1

什麼部分服務blob描述here不適合你?

+0

:這個類沒有執行(類ServeHandler),這是我的問題? – 2012-03-26 21:57:20

+0

:main()中的應用程序本身不執行。 – 2012-03-26 22:10:51

1

我知道這可能很明顯,但你的app.yaml設置爲處理該應用程序路徑?那會給你一個404.另外你爲什麼在這裏使用全球?我會避免這一點。將URL安全blob鍵作爲URL的一部分或URL參數傳遞給它,並從中獲取blob信息。如果你使用全局和appengine,你將會遇到各種潛在的問題。

我已經使用blob存儲來做各種類似文件的文件,從來沒有問題。它最有可能是你的代碼/應用程序的問題,而不是send_blob的任何錯誤。

+0

:我試過這種方法來通過URL傳遞blobinfo,但問題是我無法從類ServerHandler中的URL獲取此密鑰(信息)? – 2012-03-28 11:16:01