9

I'm programatically uploading image files and want to set the filename. When I upload a file via POST, the filename property is set automatically. However when using the method below, the filename is not getting set.

        image = urllib2.urlopen(url)
        file_name = files.blobstore.create(mime_type='image/png')
        with files.open(file_name, 'a') as f:
            f.write(image.read())
        files.finalize(file_name)  
        image_blob_key = files.blobstore.get_blob_key(file_name) 
Will Curran
  • 6,959
  • 15
  • 59
  • 92

2 Answers2

8

Parse the filename from the url (see related question here). Then you can set it by adding an additional parameter to your files.blobstore.create call:

file_name = files.blobstore.create(mime_type='image/png',_blobinfo_uploaded_filename=file_name_from_url)
Community
  • 1
  • 1
Kevin P
  • 1,655
  • 10
  • 16
  • Thanks. Would you mind linking to where you got the full signature for the create call? I could not find any docs. – Will Curran Apr 18 '11 at 02:34
  • I faced this issue not too long ago and honestly I think I had to track it down in the source code. ...\appengine\api\files\blobstore.py – Kevin P Apr 18 '11 at 02:55
  • You shouldn't rely on underscore-prefixed parameters to API functions - they may change in future. The portable solution would be to store the filename in an entity that references the BlobStore blob. – Nick Johnson Apr 18 '11 at 05:12
  • 1
    @Nick Johnson Good to know that this parameter is subject to change. My purpose in setting the filename on the blob was to allow the file to be served using that name ie - self.send_blob(blob_info,save_as=True). Is there another workaround for this functionality? – Kevin P Apr 18 '11 at 16:22
  • Just a note. The files API has been deprecated, and so this answer will cease being valid from then end of July 2015. – JohnGB Jul 18 '15 at 09:11
4

I know this is an old question but ...

self.send_blob(blob_info,save_as=True) allows you instead of True, to specify a string. What that means is that the file will be served with the provided string as the filename. So one solution is for you to keep the filename along with the blobkey, and then when you serve these using send_blob, you provide the filename as an argument. You don't care how the file will be stored, you only care how it will be served.

pitsi
  • 41
  • 1