Skip to main content
 首页 » 编程设计

python之GAE 中 BlobStore 中的 Blob

2025年05月04日9xing901022

我在 GAE BlobStore 中创建了 blob,并且这些文件创建成功,问题是当我尝试使用 BlobKey 提供这些文件时,我得到了 content-length=0 如下:

Status: 200 OK 
Cache-Control: no-cache 
X-AppEngine-BlobKey: crXwVb6vKoS8OykvgPmSew== 
Content-Type: application/zip 
Content-Disposition: attachment; filename="test.zip" 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Content-Length: 0 

例如 (test.zip) 是在 BlobStore 中创建的文件,我在管理控制台中检查了 BlobStore 并成功创建了该文件。 编辑: 下载.py代码:

def mime_type(filename): 
    return guess_type(filename)[0] 
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
    def get(self): 
 
        blob_key = self.request.get('key') 
        blob_key = str(urllib.unquote(blob_key)) 
        blob_info = blobstore.BlobInfo.get(blob_key) 
        content_type1 =mime_type(blob_info.filename) 
        save_as1 =  blob_info.filename 
        self.send_blob(blob_key,content_type=content_type1,save_as=save_as1) 
 
 
 
def main(): 
 
    application = webapp.WSGIApplication([ 
            (r'/download.*', ServeHandler), 
        ], debug=True) 
    run_wsgi_app(application) 
 
 
 
if __name__ == '__main__': 
    main() 

key 存在于 URL 中:

http://localhost:8080/download.py?key=Es9f00P29wNTZoeL9ccS4g== 

然后我从 blobstore 获取 blob。

提前致谢。

请您参考如下方法:

根据文档字符串:

Args: 
  blob_key_or_info: BlobKey or BlobInfo record to serve. 
  content_type: Content-type to override when known. 
  save_as: If True, and BlobInfo record is provided, use BlobInfos 
    filename to save-as.  If string is provided, use string as filename. 
    If None or False, do not send as attachment. 
  start: Start index of content-range to send. 
  end: End index of content-range to send.  End index is inclusive. 
  use_range: Use provided content range from requests Range header. 
    Mutually exclusive to start and end. 

尝试将您的代码更改为

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
  def get(self): 
    blob_key = BlobKey(urllib.unquote(self.request.get('key'))) 
    self.send_blob(blob_key, save_as=True)