1

I've used this question as a template to solve the same problem, but I'm running into issues when posting. I have these components:

  1. HTML form with a textbox for the image URL. This posts to...
  2. A handler that takes the posted URL, encodes it, and uses urlfetch to post it again to...
  3. A separate file upload handler that does the actual saving.

Component #3 works fine by itself if I use a file input. But I don't quite understand how to get urlfetch what it needs from just the image URL. My process either times out or gets a 500 response from the final handler.

# 1
class URLMainHandler(RequestHandler):
    def get(self):
        return render_response('blob/upload_url.html', 
                               upload_url=url_for('blobstore/upload/url'))
# 2        
class URLUploadHandler(RequestHandler):
    def post(self):
        import urllib
        # Get the posted image URL. 
        data = urllib.urlencode({'file': self.request.form.get('file')})
        # Post image to blobstore by calling POST on the file upload handler. 
        result = urlfetch.fetch(url=blobstore.create_upload_url(url_for('blobstore/upload')),
                                payload=data, 
                                method=urlfetch.POST)

        return self.redirect(url_for('blobstore/url'), result.status_code)

# 3
class UploadHandler(RequestHandler, BlobstoreUploadMixin):
    def post(self):
        # 'file' is the name of the file upload field in the form.
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        response = redirect_to('blobstore/serve', resource=blob_info.key())
        # Clear the response body.
        response.data = ''
        return response

Again, this is the process I'm following. Thanks for your help!

Community
  • 1
  • 1
Matt Norris
  • 8,596
  • 14
  • 59
  • 90

2 Answers2

3

You can achieve the same without using blobstore api. I think you have to just get the url and get the content using urlfetch().content method and store it as a blob property.

url = "imageurl"
result = urlfetch.fetch(url)
if result.status_code == 200:
   prof.avatar = db.Blob(result.content)

For further reference in storing and serving images from datastore as blob.

You can see this post for more on store-images-in-datastore

Community
  • 1
  • 1
Abdul Kader
  • 5,781
  • 4
  • 22
  • 40
  • Thanks - this is much simpler. I also found this a good resource for serving images: http://code.google.com/appengine/articles/python/serving_dynamic_images.html – Matt Norris Mar 23 '11 at 02:33
3

You can't just include the image as the payload of the blobstore HTTP request and expect it to understand what to do with it. The blobstore expects an application/multipart-form-data type message, which is what the browser provides when you upload to the blobstore. There's a library that does this for you here.

A future release of the SDK will include the ability to programmatically store blobs in the blobstore, which avoids the need for this nasty hack.

If your images are less than 1MB in size, though, a much simpler solution is to store the image directly in the datastore, as Abdul suggests in his answer.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • Thanks, Nick. Is it possible to mix and match the Blobstore to serve the images once they're uploaded so you don't have to make custom handlers for each model? An example is here http://stackoverflow.com/questions/4763715/tipfy-how-to-display-blob-in-template/4767873#4767873, but it didn't work for me. After all, a BlobProperty does not have a key. Is there a function in the Blobstore that would bridge the gap? – Matt Norris Mar 23 '11 at 02:32
  • 1
    @Wraith No, you can only use the blobstore to serve images that are stored in the blobstore. You could create a single model just to store images, though, and use a ReferenceProperty to refer to images from other models. – Nick Johnson Mar 23 '11 at 03:03
  • used the hack suggested.its giving a error of this king............. the RequestTooLargeError: The request to API call file.Append() was too large. thanks for the post we hope the api access for the same would be available soon...... – sandeep koduri Aug 13 '11 at 10:57
  • @sandeep You need to break the write up into multiple chunks - individual API calls can be no more than 1MB. – Nick Johnson Aug 14 '11 at 06:08