4

Once you upload a file to the blobstore, it renames it something like "s9QmBqJPuiVzWbySYvHVRg==". If you navigate to its "/serve" URL to download the file, the downloaded file is named this jumble of letters.

Is there a way to have the downloaded file retain its original filename when uploaded?

kennysong
  • 2,044
  • 6
  • 24
  • 37

3 Answers3

6

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

In the blob serve handler, you can specify that the blob should be returned as download attachment, and you can specify with what name should it be saved 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)
tzador
  • 2,535
  • 4
  • 31
  • 37
  • 1
    Is there a way to specify blob_info.filename when using the experimental API to write a file directly to the blobstore? – kennysong Aug 30 '12 at 17:40
  • 1
    There is a relevant question http://stackoverflow.com/questions/5697844/how-to-set-filename-property-in-blobstore – topless Aug 30 '12 at 18:16
  • 1
    Yes, when writing a blob through file api, you can specify it a name, here is how: file_name = files.blobstore.create( mime_type=content_type, _blobinfo_uploaded_filename=blob_name, ) – tzador Aug 30 '12 at 20:22
0

In the GAE admin console, BLOB viewer section, when you view an individual BLOB there is a download button on the bottom right of the viewer, as shown in the screenshot below.

enter image description here

Lynn Langit
  • 4,030
  • 1
  • 23
  • 31
  • I wonder if it dominoes always show that download button. I have a file of type (application/x-compress) which also says its too large for a preview - there is no download link. – nwaltham Jan 12 '13 at 18:59
0

The code that you refer to is the key of the BlobInfo entity, but the original filename is stored as property.

If you want a simple way to download a file by its filename you can use this code that I use for my ServeHandler, it works for my needs, download a file by its filename instead of 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)