GAE中的Send_blob

时间:2012-03-26 22:34:28

标签: python google-app-engine blobstore

我在GAE的blobstore中创建了zip文件,然后我尝试使用以下代码获取(下载)此zip文件:

def send_blob(blob_key_or_info, content_type=None, save_as=None):

    CONTENT_DISPOSITION_FORMAT = "attachment; filename=\"%s\""

    if isinstance(blob_key_or_info, blobstore.BlobInfo):
        blob_key = blob_key_or_info.key()
        blob_info = blob_key_or_info
    else:
        blob_key = blob_key_or_info
        blob_info = None
    if blob_info:
        content_type = content_type or mime_type(blob_info.filename)
        save_as = save_as or blob_info.filename
        #print save_as
    logging.debug(blob_info)
    response = Response()
    response.headers[blobstore.BLOB_KEY_HEADER] = str(blob_key)
    if content_type:
        if isinstance(content_type, unicode):
            content_type = content_type.encode("utf-8")
        response.headers["Content-Type"] = content_type
    else:
        del response.headers["Content-Type"]

    def send_attachment(filename):
        if isinstance(filename, unicode):
            filename = filename.encode("utf-8")
        response.headers["Content-Disposition"] = (\
            CONTENT_DISPOSITION_FORMAT % filename)
    if save_as:
        if isinstance(save_as, basestring):
            send_attachment(save_as)
        elif blob_info and save_as is True:
            send_attachment(blob_info.filename)
        else:
            if not blob_info:
                raise ValueError("Expected BlobInfo value for blob_key_or_info.")
            else:
                raise ValueError("Unexpected value for save_as")
    return response

如果我在main中调用此函数并从此函数(响应)返回print返回值,我会得到例如: 200好的 内容长度:0 X-AppEngine-BlobKey:C25nn_O04JT0r8kwHeabDw == 内容类型:应用程序/ zip 内容 - 处理:附件;文件名=" test.zip" 但问题是我现在如何使用此响应将文件存入我的PC(下载它)? 提前谢谢。

2 个答案:

答案 0 :(得分:3)

您需要实现Blobstore下载处理程序来提供文件。例如:

from google.appengine.ext.webapp import blobstore_handlers

class ServeZip(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self):
    blob_key = self.request.get('key')
    if not blobstore.get(blob_key):
      logging.info('blobstore.get(%s) failed' % blob_key)
      self.error(404)
      return

    self.send_blob(blob_key)
    return

然后在客户端上拨打:http://yourapp.appspot.com/servezip?key=<your_url_encoded_blob_key>

对于上面的示例:http://yourapp.appspot.com/servezip?key=C25nn_O04JT0r8kwHeabDw%3D%3D

答案 1 :(得分:2)

Google为处理BlobStore对象提供了非常好的api,主要是名为BlobstoreDownloadHandler和BlobstoreUploadHandler的Two Clsses。

要下载内容,请尝试使用BlobstoreDownloadHandler,以下代码可帮助您理解该概念。

    from google.appengine.ext.webapp import blobstore_handlers
    from google.appengine.ext.blobstore import BlobKey

    class VideoDownloadHelper(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, blobkey):
            blobKey = BlobKey(blobkey)
            #content_type is optional and by default it is same as uploaded content's content-type.
            self.send_blob(blobKey, content_type="image/jpeg")

这个方法可以像

一样使用
    app = webapp2.WSGIApplication([(r'/download-video/([^\.]+)', VideoDownloadHandler)])

进一步阅读,你可以通过这个 https://cloud.google.com/appengine/docs/python/blobstore/