Referencing Blobs in GAE the smart way
I'm migrating a site from IIS to GAE and am trying to batch upload a lot of images. Further to that particular challenge, I have one that concerns me more. All of the content for the site (stored in a DB) reference the images as such:
http://myurl.com/images/some-folder/maybe-another-folder/image-name.jpg
I have created a simple handler in Python to serve images:
class ServeBlobs(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
query = "where filename='%s'" % resource
blobs = blobstore.BlobInfo.gql(query).fetch(1)
self.send_blob(blob_infos[0])
and the 'route' is as follows
('/images/(.*)', ServeBlobs)
This works great and doesn't require a separate lookup table. However, I'm pretty sure the filename property of hte Blob can't contain '/' so what do I do about the folders (of which there are many) called some-folder or maybe-another-folder in the example above?
Furthermore, there is almost certainly overlap in image names in different folders, e.g.
"/images/foldera/main.jpg"
and
"/images/folderb/main.jpg"
开发者_如何学C
Any ideas?
You should have the upload form specify a complete path to the file as an additional field, and either ignore the upload filename entirely, or append it to the provided path.
Then, you should create a Datastore model that has a BlobReferenceProperty
referencing the blob, with the full path as the key name. That way, you can look up a blob via a simple datastore get instead of a more expensive query, and you can additionally store any relevant information against the entity (such as header information, etc).
Not a direct answer but more a recommendation:
If it is at all possible within your application, I would recommend trying to migrate all your dynamic images to use the blobstore's get_serving_url method. Rather than having a handler to manually serve your blobs. It is a much more efficient system for serving images and gives you the future flexibility of accessing the builtin resizing.
This would require your migration to be somewhat more complicated, but I believe much better in the long run.
- You would need to POST each of your images to the blobstore as you currently do and retrieve the serving url for the image blob
- Keep a local map of your old-urls => new-serving-urls during migration
- update all the old-urls to your new-serving-urls within your application/data
depending on how you stored all your links to images in your previous version of your app this could be trivial or near impossible, but I think it's worth keeping in mind.
精彩评论