如何从处理程序中的blobstore检索文件对象?

时间:2012-04-21 13:03:29

标签: python google-app-engine blobstore

我已经在blobstore中保存了一个文档,并且我试图在处理程序(处理任务)中检索它。我已经阅读了the appengine documentation关于如何使用blobstore的内容,但我正在努力让它适用于我的案例。我在处理程序中尝试了以下内容,但似乎无法将对象作为已保存的文件返回(例如.pdf或.txt)

class SendDocuments(webapp2.RequestHandler):
    def post(self):
        document_key = self.request.get("document_key")
        document_key = Key(str(document_key))
        the_document = DocumentsModel.all().filter("__key__ =", document_key).get()
        file_data = blobstore.BlobInfo.get(str(the_document.blobstore_key)) # returns a blobinfo object
        file_data.open() # returns a blobreader object
        file_data.open().read() # returns a string

我也试过

class ServeSavedDocument(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, blob_key):
    self.send_blob(blob_key, save_as=True)
    return

class SendDocuments(webapp2.RequestHandler):
    def post(self):
        document_key = self.request.get("document_key")
        document_key = Key(str(document_key))
        the_document = DocumentsModel.all().filter("__key__ =", document_key).get()
        grab_blob = ServeSavedDocument()
        file_data = grab_blob.get(self, str(the_document.blobstore_key))

但是对ServeSavedDocument的调用失败了

'NoneType' object has no attribute 'headers'

我已经查看了files api但是唯一没有保存文件的例子似乎只是返回blob键,即

blob_key = files.blobstore.get_blob_key(file_name)    

从处理程序中获取blobstore中保存文件的最佳方法是什么?

编辑1:

我正在尝试从blobstore中检索txt文件或pdf文件,格式/状态可以使用以下代码在post请求中编码为文件

from google.appengine.api import urlfetch
from poster.encode import multipart_encode
# assuming here that file_data is the file object
payload = {}
payload['user_id'] = '1234123412341234'
payload['test_file'] = MultipartParam('test_file', filename=file_data.filename,
                                      filetype=file_data.type,
                                      fileobj=file_data.file)
data,headers= multipart_encode(payload)
send_url = "http://127.0.0.0/"
t = urlfetch.fetch(url=send_url, payload="".join(data), method=urlfetch.POST, headers=headers)

3 个答案:

答案 0 :(得分:4)

好吧,在经历了很多混乱之后,我发现这很有效!关键是简单地在blobinfo对象上调用open()。

class SendDocuments(webapp2.RequestHandler):
    def post(self):
        document_key = self.request.get("document_key")
        document_key = Key(str(document_key))
        the_document = DocumentsModel.all().filter("__key__ =", document_key).get()
        file_data = blobstore.BlobInfo.get(str(the_document.blobstore_key))
        payload = {}
        payload['user_id'] = '1234123412341234'
        payload['test_file'] = MultipartParam('the_file', filename="something",
                                      filetype=file_data.content_type,
                                      fileobj=file_data.open())

答案 1 :(得分:0)

看一下BlobReader类,我认为这就是你要找的东西: https://developers.google.com/appengine/docs/python/blobstore/blobreaderclass

它允许对blobstore数据进行类似文件的读访问:

# blob_key = ..

# Instantiate a BlobReader for a given Blobstore value.
blob_reader = blobstore.BlobReader(blob_key)
# Read the entire value into memory. This may take a while depending
# on the size of the value and the size of the read buffer, and is not
# recommended for large values.
value = blob_reader.read()

答案 2 :(得分:0)

尝试这样做,在send_blob()中指定文件名及其类型:

def mime_type(filename):
    return guess_type(filename)[0]
    class ServeSavedDocument(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self):
            blob_key = self.request.get("blob_key")
            if blob_key:
                blob_info = blobstore.get(blob_key)               
                if blob_info:
                    save_as1 =  blob_info.filename
                    type1=mime_type(blob_info.filename)
                    self.send_blob(blob_info,content_type=type1,save_as=save_as1)
相关问题