2011-06-03 74 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() 
相关问题