【问题标题】:Send_blob in GAE在 GAME 中发送 Blob
【发布时间】:2012-04-10 11:20:19
【问题描述】:

我在 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 中调用这个函数并从这个函数(响应)打印返回值,我会得到例如: 200 好 内容长度:0 X-AppEngine-BlobKey:C25nn_O04JT0r8kwHeabDw== 内容类型:应用程序/zip 内容处置:附件;文件名="test.zip" 但是问题是我现在如何使用此响应将文件获取到我的 PC(下载)? 提前致谢。

【问题讨论】:

    标签: python google-app-engine blobstore


    【解决方案1】:

    您需要实现 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

    【讨论】:

    • :当我将这个类处理程序放在单独的页面中然后将密钥传递给它时,问题是:状态:404 未找到内容类型:文本/html; charset=utf-8 Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 0, 处理程序没有执行。对这个问题有任何想法吗?
    • 您是否在日志中看到“blobstore.get(...) failed”错误?如果是这样,您可能传递了错误的密钥或未能正确地对密钥进行 URL 编码。如果没有,您可能没有在 app.yaml 中正确添加处理程序。
    【解决方案2】:

    Google 提供了非常好的 api 来处理 BlobStore 对象,主要是 BlobstoreDownloadHandler 和 BlobstoreUploadHandler 两个类。

    要下载内容,请尝试使用 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2013-06-04
      • 2015-02-13
      • 2012-04-08
      • 1970-01-01
      相关资源
      最近更新 更多