2012-10-22 32 views
1

我最近开始使用奶瓶和GAE Blob存储,虽然我可以将文件上传至Blobstore我似乎无法找到一种方法来从商店下载。使用bottle.py和Blob存储GAE

我遵循文档中的示例,但只在上传部分成功。由于我使用的是webapp/2中的其他框架,因此无法将该示例集成到我的应用程序中。

我将如何去创造一个上传处理程序和下载处理程序,以便我可以上传Blob的密钥并将其存储在我的数据模型,后来在下载处理使用它?

我尝试使用BlobInfo.all()创建查询Blob存储区,但我不能够得到实体的键名称字段值。

这是我与Blob存储区进行的首次互动,所以我不会在一个更好的解决问题的方法介意建议。

回答

1

对于服务一个blob我建议你看看source code of the BlobstoreDownloadHandler。将它移植到瓶子应该很容易,因为没有什么特别的框架。

下面是关于如何使用BlobInfo.all()一个例子:

for info in blobstore.BlobInfo.all(): 
    self.response.out.write('Name:%s Key: %s Size:%s Creation:%s ContentType:%s<br>' % (info.filename, info.key(), info.size, info.creation, info.content_type)) 
0

你才真正需要生成包括报头的响应下载“X-AppEngine上-的BlobKey:你blob_key]”一切沿否则,如果需要,您需要像Content-Disposition标题。或者如果它是一个图像,你应该只是使用高性能图像服务api,生成一个url并重定向到它....完成

上传,除了编写一个处理程序的appengine调用一次上传是安全的Blob存储区(这是在文档)

您需要一种方法来查找传入请求的BLOB信息。我不知道瓶子里的要求是什么样子。 Blobstoreuploadhandler有一个get_uploads方法,据我所知,它没有任何理由需要成为实例方法。所以这是一个示例泛型实现,它需要一个webob请求。对于瓶子,您需要写一些与瓶子请求对象相似的东西。

def get_uploads(request, field_name=None): 
    """Get uploads for this request. 
    Args: 
     field_name: Only select uploads that were sent as a specific field. 
     populate_post: Add the non blob fields to request.POST 
    Returns: 
     A list of BlobInfo records corresponding to each upload. 
     Empty list if there are no blob-info records for field_name. 

    stolen from the SDK since they only provide a way to get to this 
    crap through their crappy webapp framework 
    """ 
    if not getattr(request, "__uploads", None): 
     request.__uploads = {} 
     for key, value in request.params.items(): 
      if isinstance(value, cgi.FieldStorage): 
       if 'blob-key' in value.type_options: 
        request.__uploads.setdefault(key, []).append(
         blobstore.parse_blob_info(value)) 

    if field_name: 
     try: 
      return list(request.__uploads[field_name]) 
     except KeyError: 
      return [] 
    else: 
     results = [] 
     for uploads in request.__uploads.itervalues(): 
      results += uploads 
     return results 
0

为寻找这个答案在未来,要做到这一点,你需要一瓶(D'哦!)和defnull的multipart模块。

由于创建上传URL一般都很简单,并且按照GAE文档,我只会介绍上传处理程序。

from bottle import request 
from multipart import parse_options_header 
from google.appengine.ext.blobstore import BlobInfo 

def get_blob_info(field_name): 
    try: 
     field = request.files[field_name] 
    except KeyError: 
     # Maybe form isn't multipart or file wasn't uploaded, or some such error 
     return None 
    blob_data = parse_options_header(field.content_type)[1] 
    try: 
     return BlobInfo.get(blob_data['blob-key']) 
    except KeyError: 
     # Malformed request? Wrong field name? 
     return None 

对不起,如果在代码中有任何错误,它是关闭我的头顶。