How to set filename property in BlobStore?
Asked Answered
B

2

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) 
Briar answered 18/4, 2011 at 1:59 Comment(0)
A
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)
Arabinose answered 18/4, 2011 at 2:30 Comment(5)
Thanks. Would you mind linking to where you got the full signature for the create call? I could not find any docs.Briar
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.pyArabinose
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.Lonnie
@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?Arabinose
Just a note. The files API has been deprecated, and so this answer will cease being valid from then end of July 2015.Hinduism
S
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.

Shiflett answered 30/8, 2011 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.