将通过Flask收到的图像上传到Firebase存储 [英] Upload image received via Flask to Firebase Storage

查看:75
本文介绍了将通过Flask收到的图像上传到Firebase存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像上传到Firebase存储.该图像通过HTTP Post传递.使用Flask处理请求.我似乎无法正确上传图片.当我指定图像的位置时,我可以成功上传图像,排除了Firebase或该代码的问题.我试图获取文件位置,但是不幸的是,Flask为安全起见阻止了此操作(可能是一件好事).在存储图像之前我需要做些处理吗?.

I am trying to upload an image to Firebase storage. The image is passed via HTTP Post. The request is processed using Flask. I can't seem to get the image to upload correctly. When I specify the image's location I can successfully upload the image, ruling out the issue with Firebase or that code. I have tried to get the file location, but unfortunately Flask has prevented this for safety(Probably a good thing). Is there something I need to do to process the image before storing it?.

我正在使用Postman发送POST请求.

I am using Postman to send the POST request.

@message_api.route('/messenger/message/send/picture/individual', methods=['POST'])
def send_individual_picture():
    picture = request.files['picture']

    firebase.storage().put(picture)

推荐答案

由于 firebase.storage().put()需要文件路径,因此您需要将上传文件保存到首先保存文件.

Since firebase.storage().put() expects a file path, then you'll need to save the upload to a file first before you can store it.

现在您拥有这个:

@message_api.route('/messenger/message/send/picture/individual', methods=['POST'])
def send_individual_picture():
    picture = request.files['picture']

    firebase.storage().put(picture)

此后, picture 是Werkzeug的

After this, picture is an instance of Werkzeug's Filestorage class, which can be treated somewhat like a file descriptor. You can do stuff like read() from it.

由于您的代码确实将文件视为瞬态文件,因此您以后不需要挂起文件,因此可以使用

Since your code is really treating the file as transient and you don't need it hanging around afterward, you can use a NamedTemporaryFile which creates a temporary file with a name. You can then delete the temporary file afterward.

import os
import tempfile

@message_api.route('/messenger/message/send/picture/individual', methods=['POST'])
def send_individual_picture():
    picture = request.files['picture']

    temp = tempfile.NamedTemporaryFile(delete=False)
    picture.save(temp.name)
    firebase.storage().put(temp.name)

    # Clean-up temp image
    os.remove(temp.name)

这篇关于将通过Flask收到的图像上传到Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆