Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloaded filename with Google App Engine Blobstore

I'm using the Google App Engine Blobstore to store a range of file types (PDF, XLS, etc) and am trying to find a mechanism by which the original filename of the uploaded file - as stored in blob_info - can be used to name the downloaded file i.e. so that the user sees 'some_file.pdf' in the save dialogue rather than 'very_long_db_key.pdf'.

I can't see anything in the docs that would allow this:

http://code.google.com/appengine/docs/python/blobstore/overview.html

I've seen hints in other posts that you could use the information in blob_info to set the content-disposition header. Is this the best approach to achieving the desired end?

like image 652
notreadbyhumans Avatar asked Sep 06 '11 19:09

notreadbyhumans


People also ask

What is a blobstore?

The Blobstore API allows your application to serve data objects, called blobs, that are much larger than the size allowed for objects in the Datastore service. Blobs are useful for serving large files, such as video or image files, and for allowing users to upload large data files.

What is Google App Engine launcher?

Google App Engine (GAE) is a platform-as-a-service product that provides web app developers and enterprises with access to Google's scalable hosting and tier 1 internet service. GAE requires that applications be written in Java or Python, store data in Google Bigtable and use the Google query language.

What is blob name in Google Cloud Storage?

The name of the blob. This corresponds to the unique path of the object in the bucket. If bytes, will be converted to a unicode object. Blob / object names can contain any sequence of valid unicode characters, of length 1-1024 bytes when UTF-8 encoded.

How do I download Google App Engine SDK?

You can download the Google App Engine SDK by going to: http://code.google.com/appengine/downloads.html and download the appropriate install package. Download the Windows installer – the simplest thing is to download it to your Desktop or another folder that you remember.


2 Answers

There is an optional 'save_as' parameter in the send_blob function. By default this is set to False. Setting it to True will cause the file to be treated as an attachment (ie it will trigger a 'Save/Open' download dialog) and the user will see the proper filename.

Example:

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info,save_as=True)

It is also possible to overwrite the filename by passing in a string:

self.send_blob(blob_info,save_as='my_file.txt')

If you want some content (such as pdfs) to open rather than save you could use the content_type to determine the behavior:

blob_info = blobstore.BlobInfo.get(resource)
type = blob_info.content_type
if type == 'application/pdf':       
    self.response.headers['Content-Type'] = type
    self.send_blob(blob_info,save_as=False)
else:
    self.send_blob(blob_info,save_as=True)
like image 119
Kevin P Avatar answered Sep 28 '22 11:09

Kevin P


For future reference, save_as and the BlobstoreDownloadHandler is documented here:

http://code.google.com/appengine/docs/python/tools/webapp/blobstorehandlers.html

It does seem like it should be a bit easier to find. Let's see if it can be improved.

like image 25
Rafe Kaplan Avatar answered Sep 28 '22 12:09

Rafe Kaplan