如何将图像(base64编码)上载到firebase?

时间:2019-08-23 03:38:39

标签: python-3.x firebase base64 firebase-storage

我有一张图片(已编码为base64)。我想使用python将这张图片上传到Firebase。我该如何实现?

当我将图像上传到Firebase时,就可以了。但是图像的类型是文本/纯文本。当我打开文件时,它就是图像。所以我要实现的是要图像文件的类型。那就是我尝试过的方式。

        if attachment_ids:
            self.env['firebase'].database_initialize()
            attach_id = attachment_ids[0][1]
            attach_obj = self.env['ir.attachment'].browse(attach_id)
            file = attach_obj.datas
            decoded_file = base64.b64decode(file)
            file_name = attach_obj.name

            if '.jpeg' in file_name:
                type = 'image'
            elif '.png' in file_name:
                type = 'image'
            elif '.jpg' in file_name:
                type = 'image'
            else:
                type = 'file'

            bucket = storage.bucket()

            if type == 'image':
                blob = bucket.blob('message_images/' + file_name)
            else:
                blob = bucket.blob('message_files/' + file_name)
            blob.upload_from_string(decoded_file)

1 个答案:

答案 0 :(得分:1)

我相信您只需在上传调用中添加一个content_type参数,例如:

blob.upload_from_string(decoded_file, content_type='image/png')