2011-06-03 79 views
4

我的問題圍繞着一個用戶將文本文件上傳到我的應用程序。我需要獲取該文件並在將其保存到數據存儲之前使用我的應用程序處理它。從我已閱讀的小內容中,我瞭解到用戶上傳直接以blob的形式直接訪問數據存儲區,如果我能夠獲取該文件,對其執行操作(意思是更改內部數據),然後將其重新寫回到數據存儲。所有這些操作都需要由應用程序完成。 不幸的是,從數據存儲文檔中,http://code.google.com/appengine/docs/python/blobstore/overview.html 應用程序無法在數據存儲中直接創建blob。這是我的頭痛。我只需要從我的應用程序中創建數據存儲中新的Blob /文件,而無需任何用戶上傳交互。如何操作谷歌應用程序引擎數據存儲中的文件

+0

見http://code.google.com/appengine/docs/python/blobstore/overview.html#Writing_Files_to_the_Blobstore;您現在可以使用文件API以編程方式寫入Blobstore。 (注意:這是在同一頁上,說你不能以編程方式創建blob;爲了讓文檔保持最新:) :) – geoffspear 2011-06-03 13:59:09

回答

2

blobstore != datastore

你可以閱讀和,只要你喜歡,只要你的數據是< 1MB您實體使用db.BlobProperty儘可能多的數據寫入到數據存儲

由於Wooble意見,新File API讓你寫的Blob存儲區,但除非你正在使用的任務或類似的東西映射精簡庫你是1MB的API調用限制仍然有限增量書面方式向Blob存儲文件讀/寫。

+1

此外,如果你明確地使用blobstore上傳,用戶上傳只能直接進入blobstore - 否則它們會像其他任何形式一樣被髮送到您的應用程序。 – 2011-06-04 02:27:56

2

感謝您的幫助。經過許多不眠之夜,3個App Engine書籍和大量Google搜索,我找到了答案。下面是代碼(所以它應該是自我解釋):

from __future__ import with_statement 
from google.appengine.api import files 
from google.appengine.ext import blobstore 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import util 

class MainHandler(webapp.RequestHandler): 
    def get(self): 
     self.response.out.write('Hello WOrld') 
     form=''' <form action="/" method="POST" enctype="multipart/form-data"> 
Upload File:<input type="file" name="file"><br/> 
<input type="submit"></form>''' 
     self.response.out.write(form) 
     blob_key="w0MC_7MnZ6DyZFvGjgdgrg==" 
     blob_info=blobstore.BlobInfo.get(blob_key) 
     start=0 
     end=blobstore.MAX_BLOB_FETCH_SIZE-1 
     read_content=blobstore.fetch_data(blob_key, start, end) 
     self.response.out.write(read_content) 
    def post(self): 
     self.response.out.write('Posting...') 
     content=self.request.get('file') 
     #self.response.out.write(content) 
     #print content 
     file_name=files.blobstore.create(mime_type='application/octet-stream') 
     with files.open(file_name, 'a') as f: 
      f.write(content) 
     files.finalize(file_name) 
     blob_key=files.blobstore.get_blob_key(file_name) 
     print "Blob Key=" 
     print blob_key 

def main(): 
    application=webapp.WSGIApplication([('/', MainHandler)],debug=True) 
    util.run_wsgi_app(application) 

if __name__=='__main__': 
    main() 
相關問題