【问题标题】:Storing images in google datastore using Flask (Python)使用 Flask (Python) 在谷歌数据存储中存储图像
【发布时间】:2011-04-30 16:39:35
【问题描述】:

我在谷歌应用程序引擎上使用烧瓶,我急切地寻求帮助来解决这个问题。 GAE 文档谈到使用 BlobProperty 将图像存储在数据存储中,应该这样做:-

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

现在应该通过以下操作将图像存储在数据存储中:-

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

但我无法做到这一点。当我得到 db.Blob 接受一个 string ,但给定一个 Filestorage 对象...有人可以帮我解决这个问题。另外,如果有人可以提示我如何在上传后将图像流回。

【问题讨论】:

  • 请考虑发布您的解决方案并将其标记为答案。这样问题就得到了正式的回答:)
  • 感谢您指出 WoLpH 。完成!

标签: python image google-app-engine flask


【解决方案1】:

我有以下型号

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

并使用下一个代码存储它:

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')

要呈现缩略图,您可以使用下一个代码:

@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

【讨论】:

    【解决方案2】:

    好的,这就是我最终解决的方法:-

    @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')
    

    上面将文件作为blob存储在数据存储中,然后可以通过以下函数检索:-

    @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)
    

    【讨论】:

      【解决方案3】:

      您应该考虑使用BlobStore 来存储您的数据。而不是db.Blob,您将使用blobstore.BlobReferencePropertyhttp://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#BlobReferenceProperty

      上传和下载非常简单,如下所示:http://code.google.com/appengine/docs/python/blobstore/overview.html#Complete_Sample_App

      【讨论】:

      • 但是对于blobstore,我不需要激活支付机制。截至目前,我只是在开发、部署、更改、处理等等。
      • @Alice;是的,这是真的。不过,启用计费不会自动让您花钱,默认配额甚至允许大多数小型网站免费运行。如果启用付款是我建议的一个选项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2017-12-24
      • 2013-08-06
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多