3

I am using flask on google app engine and am desperately looking for help to solve this. The GAE documentation talks of storing images in the datastore using the BlobProperty , which should be done something like this:-

class MyPics(db.Model):
      name=db.StringProperty()
      pic=db.BlobProperty()

Now the image should be stored in the datastore by doing this:-

def storeimage():
    pics=MyPics()
    pics.name=request.form['name']
    uploadedpic=request.files['file']  #where file is the fieldname in the form of the                
                                        file uploaded
    pics.pic=db.Blob(uploadedpic)
    pics.put()
    redirect ... etc etc

But am unable to do this. as I get db.Blob accepts a string , but given a Filestorage object... Can someone help me with this. Also if anybody could hint me on how to stream the image back after uploading.

1
  • 1
    please consider posting your solution and marking it as the answer. That way the question is officially answered :) Commented Nov 3, 2010 at 14:01

3 Answers 3

6

Ok so this is how I finally solved it:-

@userreg.route('/mypics',methods=['GET','POST'])
def mypics():    
   if request.method=='POST':
      mydata=MyPics()
      mydata.name=request.form['myname']
      file=request.files['file']
      filedata=file.read()
      if file:
         mydata.pic=db.Blob(filedata)
      mydata.put()
      return redirect(url_for('home'))
   return render_template('mypicform.html')

The above stores the file as a blob in the datastore and then it can be retrieved by the below func:-

@userreg.route('/pic/<name>')
def getpic(name):
     qu=db.Query(MyPics).filter('name =',name).get()
     if qu.pic is None:
         return "hello"
     else:
         mimetype = 'image/png'
         return current_app.response_class(qu.pic,mimetype=mimetype,direct_passthrough=False)
Sign up to request clarification or add additional context in comments.

Comments

1

You should consider using the BlobStore to store your data. Instead of a db.Blob you would be using blobstore.BlobReferenceProperty: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#BlobReferenceProperty

Uploading and download is quite easy as shown here: http://code.google.com/appengine/docs/python/blobstore/overview.html#Complete_Sample_App

2 Comments

But for blobstore wont I have to activate the payment mechanism. As of now I am just developing, deploying, changing things, working on it etc etc.
@Alice; Yes, that's true. Enabling billing doesn't automatically cost you money though, the default quotas allow even most small websites to run for free. If enabling payments is an option that that's what I'd suggest.
0

I have following model

class Picture(db.Model):    
    data = db.BlobProperty()
    ext = db.StringProperty()
    content_type = db.StringProperty()

and store it using next code:

def create_picture():
    if request.files['file']:                       
        picture.data = request.files['file'].read()
        picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
        picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'File uploaded', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/new.html', title=u'Upload a picture', message=u'No picture selected')

To render a thumbnail you can use next code:

@frontend.route('/thumbnail/<id>/<width>x<height>.<ext>')
def thumbnail(id, width, height, ext):
    key = db.Key(id)
    picture = Picture.get(key)
    img = images.Image(picture.data)

    if width != '0':
        w = int(width)
    else:
        w = img.width

    if height != '0':
        h = int(height)
    else:
        h = img.height

    if img.height > h and h != 0:
        w = (int(width) * img.width) / img.height;

    if img.width > w:
        h = (int(height)  * img.height) / img.width;

    thumb = images.resize(picture.data, width=w, height=h)    
    response = make_response(thumb)
    response.headers['Content-Type'] = picture.content_type
    return response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.