views:

197

answers:

3

I'm working on a Google App Engine application, and have come to the point where I want to associate images on the filesystem to entities in the database.

I'm using the bulkupload_client.py script to upload entities to the database, but am stuck trying to figure out how to associate filesystem files to the entities. If an entity has the following images: main,detail,front,back I think I might want a naming scheme like this: <entity_key>_main.jpg

I suppose I could create a GUID for each entity and use that, but I'd rather not have to do that.

Any ideas?

I think I can't use the entity key since it might be different between local and production datastores, so I would have to rename all my images after a production bulkupload.

A: 

I see two options here based on my very limited knowledge of GAE.

First, you can't actually write anything to the file system in GAE, right? That would mean that any images you want to include would have to be uploaded as a part of your webapp and would therefore have a static name and directory structure that is known and unchangeable. In this case, your idea of _main.jpg, OR /entity_key/main.jpg would work fine.

The second option is to store the images as a blob in the database. This may allow for uploading images dynamically rather than having to upload a new version of the webapp every time you need to update images. It would quickly eat into your free database space. Here's some information on serving pictures from the database. http://code.google.com/appengine/articles/images.html

digitaljoel
Yes, I would be uploading the images when I deploy the project. I think I can't use the entity key since it might be different when I bulkupload to my dev datastore and the production datastore.
MStodd
yeah, you would need some sort of identifier that you have control over and is guaranteed not to change.
digitaljoel
+1  A: 

There is a GAE tutorial on how to Serve Dynamic Images with Google App Engine. Includes explanations & downloadable source code.

Kevin Williams
A: 

If you're uploading the images statically, you can use the key based scheme if you want: Simply assign a key name to the entities, and use that to find the associated images. You specify a key name (in the Python version) as the key_name constructor argument to an entity class:

myentity = EntityClass(key_name="bleh", foo="bar", bar=123)
myentity.put()

and you can get the key name for an entity like so:

myentity.key().name()

It sounds like the datastore entities in question are basically static content, though, so perhaps you'd be better simply encoding them as literals in the source and thus having them in memory, or loading them at runtime from local datafiles, bypassing the need to query the datastore for them?

Nick Johnson