在GAE中的Send_blob [英] Send_blob in GAE
问题描述
i created zip files into the blobstore in GAE,then i tried to get(download)this zip file using this code:
c> def send_blob(blob_key_or_info,content_type = None,save_as =无):
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
如果blob_info:
content_type = content_type或mime_type(blob_info.filename)
save_as = save_as或blob_info.filename
#print save_as
logging.debug(blob_info)
响应= 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)
响应.headers [Content-Type] = content_type
else:
del response.headers [Content-Type]
def send_attachment(filename):
如果是isinstance(filename,unicode):
filename = filename.encode(utf-8)
response.headers [Content-Disposition] =(\
CONTENT_DISPOSITION_FORMAT%filename)
如果save_as:
如果isinstance(save_as,basestring):
send_attachment(save_as)
elif blob_info和save_as为True:
send_attachment(blob_info.filename)
else:
如果不是blob_info:
则提高ValueError(Blob_key_or_info的期望BlobInfo值)
else:
提高ValueError(save_as的意外值)
返回响应
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和main函数中调用此函数并返回此函数的返回值(回应)我得到例如:
2 00 OK
Content-Length:0
X-AppEngine-BlobKey:C25nn_O04JT0r8kwHeabDw ==
Content-Type:application / zip
Content-Disposition:attachment;文件名=test.zip
但问题我现在如何使用这个响应来获取文件到我的电脑(下载它)?
在此先感谢。
and if i call this function in the main and print return return value from this function(response)i get for example: 200 OK Content-Length: 0 X-AppEngine-BlobKey: C25nn_O04JT0r8kwHeabDw== Content-Type: application/zip Content-Disposition: attachment; filename="test.zip" But the question how can i now use this response to get the file to my PC(download it)? Thanks in advance.
推荐答案
您需要实现Blobstore下载处理程序来为文件提供服务。例如:
You need to implement a Blobstore download handler to serve the file. For example:
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>
Then on the client you'd call: http://yourapp.appspot.com/servezip?key=<your_url_encoded_blob_key>
以上示例: http://yourapp.appspot。 com / servezip?key = C25nn_O04JT0r8kwHeabDw%3D%3D
这篇关于在GAE中的Send_blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!