Reputation: 403
i created zip files into the blobstore in GAE,then i tried to get(download)this zip file using this code:
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
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.
Upvotes: 0
Views: 1543
Reputation: 414
Well Google provides very good api to process BlobStore objects, mainly Two Clsses named as BlobstoreDownloadHandler and BlobstoreUploadHandler.
To download content try to use BlobstoreDownloadHandler and the below code help you to understand the concept.
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")
And this method can be use like
app = webapp2.WSGIApplication([(r'/download-video/([^\.]+)', VideoDownloadHandler)])
For further reading you can go through this https://cloud.google.com/appengine/docs/python/blobstore/
Upvotes: 2
Reputation: 701
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
Then on the client you'd call: http://yourapp.appspot.com/servezip?key=<your_url_encoded_blob_key>
For the example above: http://yourapp.appspot.com/servezip?key=C25nn_O04JT0r8kwHeabDw%3D%3D
Upvotes: 3