Jeremy Gillbert
Jeremy Gillbert

Reputation: 133

How to upload an image(base64 encoded) to firebase?

I have an image(base64 encoded). I want to upload this image to firebase using python. How can I achieve this?

When I upload the image to firebase, it is ok. But the type of the image is text/plain. When I open the file, it is the image. So what I want to achieve is the type of the file to be image. That is how I tried.

        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)

Upvotes: 2

Views: 880

Answers (1)

Michael Bleigh
Michael Bleigh

Reputation: 26353

I believe you just need to add a content_type argument to your upload call, for example:

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

Upvotes: 4

Related Questions