How to use Google App Engine's Blobstore to save web images
UPDATE
This question w开发者_运维知识库as originally asked at a time when there was no support for programmatic file creation (via url for example). That has changed, see: http://code.google.com/appengine/docs/java/blobstore/overview.html#Writing_Files_to_the_Blobstore
I'm accessing a couple different APIs to fetch images. My application is GAE + Python and I want to use the Blobstore to save these images. The GAE Blobstore documentation provides clear examples of how to save images to Blobstore via a form, but not directly from an url.
http://code.google.com/intl/iw/appengine/docs/python/blobstore/overview.html#Writing_Files_to_the_Blobstore
I want to know how to save http://api.website.com/images/foo.jpg to the Blobstore.
Using the new File API in combination with urlfetch API you should be able to pass the image url through the form, fetch it server-side and write it to the blobstore:
http://code.google.com/intl/iw/appengine/docs/python/blobstore/overview.html#Writing_Files_to_the_Blobstore
HTTP POST to your own form works. I didn't try submitting a file, but here's how I submit a form. You can get the file from the internet, add it to the form submission and the file will store in your blobstore.
import urllib
data = urllib.urlencode({"id":str(id), "password" : self.request.POST['passwd'], "edit" : "edit"})
result = urlfetch.fetch(url="http://www.montao.com.br/upload",
payload=data,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
There's also a similar question answered here
I hope it works for you.
You could write an additional handler to upload the content of an input url to the blob store handler. i.e. automating the form submission process. make a POST
request to the blob store handler and url encode the image.
But the only problem would be if the file is too large then you might get timeout exceptions.
精彩评论