How to download a file with its original name from the GAE blobstore?

Once you upload the file to blobstore, it renames it as "s9QmBqJPuiVzWbySYvHVRg ==". If you go to your "/ serve" URL to download the file, the downloaded file is called this mess of letters.

Is there a way for the downloaded file to keep the original file name at boot time?

+4
source share
3 answers

When a file is uploaded using BlobUploadHandler original file name is saved as a name property in the newly created BlobInfo entity.

In the blob service handler, you can specify that the blob should be returned as a downloadable attachment, and you can specify which name to save it with

 from google.appengine.ext import webapp import urllib class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def get(self, blob_info_key=None): blob_info_key = str(urllib.unquote(blob_info_key)) blob_info = retrieve_blob_info(blob_info_key) self.send_blob(blob_info, save_as=blob_info.filename) blob_app = webapp.WSGIApplication([ ('/_s/blob/([^/]+)', blob.ServeHandler), ], debug=config.DEBUG) 
+6
source

In the GAE console , the BLOB viewer, when you are viewing a single BLOB, in the lower right part as shown in the screenshot below.

enter image description here

0
source

The code you reference is the key of the BlobInfo object, but the original file name is saved as a property.

If you need an easy way to download a file by its name, you can use this code, which I use for my ServeHandler, it works for my needs, downloads a file by its name instead of the blobstore key:

 class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def get(self, resource): blobs = blobstore.BlobInfo.gql("WHERE filename = '%s'" %(urllib.unquote(resource))) if blobs.count(1) > 0: blob_info = blobstore.BlobInfo.get(blobs[0].key()) self.send_blob(blob_info,save_as=True) 
0
source

All Articles