加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用Flask(Python)在Google数据存储中存储图像

发布时间:2020-12-20 11:15:51 所属栏目:Python 来源:网络整理
导读:我在谷歌应用引擎上使用烧瓶,我拼命寻求帮助来解决这个问题. GAE文档讨论了使用BlobProperty在数据存储区中存储图像,这应该是这样的: – class MyPics(db.Model): name=db.StringProperty() pic=db.BlobProperty() 现在,图像应该存储在数据存储区中: def s
我在谷歌应用引擎上使用烧瓶,我拼命寻求帮助来解决这个问题.
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接受一个字符串,但给定一个Filestorage对象…有人可以帮我这个.此外,如果有人可以提示我如何在上传后将图像流回来.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读