Upload Image To Appengine Datastore Using Blobstore And Endpoints
How can I upload a file/image to the Appengine Datastore using blobStore? I'm using Google Cloud Endpoints. This is my model: class ProductImage(EndpointsModel): _message_field
Solution 1:
I couldn't figure out a way to do this with just Endpoints; I had to have a hybrid server with part-endpoints application, part-webapp2 blobstore_handlers application. If you use the webapp2 stuff as per the Blobstore upload examples for those parts, it works. For example, the flow should be:
- Client requests an upload URL (use Endpoints for this call, and have it basically do blobstore.create_upload_url(PATH)
- Client uploads image to the given URL, which is handled by your blobstore_handlers.BlobstoreUploadHandler method, which pulls out the upload and dumps the blob_info.key() (in JSON, for example)
- Client calls createProduct or whatever, an Endpoint, and passes back the blobkey it just received, along with the rest of your ProductImage model. You may want to call get_serving_url in that method and stash it in your model, it shouldn't change later.
- Clients can then use that stashed serving url to view image.
Also I had a lot of "fun" with the BlobKeyProperty. In dev deployments, everything worked fine, but in 'production', I'd get invalid image errors while calling get_serving_url() on the stored blobkey. I think this might be due to the blobs actually not being bitmaps, though, and dev not caring.
Post a Comment for "Upload Image To Appengine Datastore Using Blobstore And Endpoints"