如何使用Python Flask将文件上传到Firebase存储?

时间:2018-06-27 00:22:42

标签: python firebase file-upload flask firebase-storage

我想上传文件,但是我在stackoverflow上看到的示例基于使用blobl.upload_from_string的示例,与我使用图像的用例不匹配。

1 个答案:

答案 0 :(得分:0)

您可以从Flask的request.files中获取文件对象,并与blob.upload_from_file一起使用以上传二进制文件。

from werkzeug.utils import secure_filename

def uploadPhoto(userIDStr, file):
    path = '/bucketname/'+str(secure_filename(file.filename))
    if file: 
        try:
            bucket = storage.bucket()
            #file is just an object from request.files e.g. file = request.files['myFile']
            blob = bucket.blob(file.filename)
            blob.upload_from_file(file)
        except Exception as e:
            console.log('error uploading user photo: ' % e)

相关文档位于Blob的页面下:http://google-cloud-python.readthedocs.io/en/latest/storage/blobs.html#google.cloud.storage.blob.Blob.upload_from_file

在使用没有最详尽的用例示例的python库时,可以使用IPython浏览对象,并在TAB之后按objectname.来查看其方法和成员。