4

我在 blobstore 中保存了一个文档,并试图在处理程序(处理任务)中检索它。我已经阅读了有关如何使用 blobstore的 appengine 文档,但我很难让它适用于我的情况。我在处理程序中尝试了以下操作,但似乎无法将对象作为保存的文件(例如 .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'

我已经查看了文件 api,但唯一不保存文件的示例似乎只是返回了 blob 密钥,即

blob_key = files.blobstore.get_blob_key(file_name)    

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

编辑1:

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

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)
4

3 回答 3

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())
于 2012-04-25T19:52:40.093 回答
0

尝试这样,指定文件名,并在send_blob()as 中输入:

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)
于 2012-04-23T18:09:27.447 回答
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()
于 2012-04-23T16:15:26.337 回答