librelist archives

« back to archive

storing and serving images in google datastore

storing and serving images in google datastore

From:
alice ni
Date:
2010-10-26 @ 10:52
Hi all!
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.

Thanks

Re: storing and serving images in google datastore

From:
alice ni
Date:
2010-10-27 @ 07:28
Ok so this is how I finally did it.. this does the job , but am not sure if
it is the right way:-
>
> @app.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)

Hope it would help some other newbie like me..

Re: [flask] Re: storing and serving images in google datastore

From:
kevin beckford
Date:
2010-10-27 @ 13:27
On Wed, Oct 27, 2010 at 12:28 AM, alice ni <alice.ni19@gmail.com> wrote:
> Ok so this is how I finally did it.. this does the job , but am not sure if
> it is the right way:-
I'm unsure also, I was wondering myself how to use images + flask.
I'll try this later.  Thanks.

Re: [flask] Re: storing and serving images in google datastore

From:
alice ni
Date:
2010-10-27 @ 13:39
Hi Kevin,I am sure that this is how images are stored in the datastore if
you are not using the blobstore(but yes have to check for allowed filenames
and all, on the lines of the flask docs). However I feel the code of image
download can be worked on.

On Wed, Oct 27, 2010 at 6:57 PM, kevin beckford <chiggsy@lazyweb.ca> wrote:

> On Wed, Oct 27, 2010 at 12:28 AM, alice ni <alice.ni19@gmail.com> wrote:
> > Ok so this is how I finally did it.. this does the job , but am not sure
> if
> > it is the right way:-
> I'm unsure also, I was wondering myself how to use images + flask.
> I'll try this later.  Thanks.
>