ueeieiie
ueeieiie

Reputation: 1562

How to upload base64 image to GCloud Storage

How do you upload an image from your web to gcloud Storage?

CLIENT SIDE: I have a file browse component where I choose an image from the local drive. Then, I transform the image into a base64 string, and send it to the server.

SERVER-SIDE: Then I upload the base64 image: blob.upload_from_string(base64data, type="image/jpeg")

But when I look inside my bucket I see a 1 black pixel image.

Questions

Upvotes: 2

Views: 3253

Answers (2)

Gabriel
Gabriel

Reputation: 171

I encountered the same problem, this is what I did

import base64, re

bucket = storage.bucket()
blob = bucket.blob(path)
regex = r"(?<=data:)(.*)(?=;)"
split = image.split('base64')
format_image = re.findall(regex, split[0])[0]
base64_image = base64.b64decode(split[1])
blob.upload_from_string(base64_image, content_type=format_image)
# Opt : if you want to make public access from the URL
blob.make_public()

Upvotes: 4

ueeieiie
ueeieiie

Reputation: 1562

In the end what i did was to remove the first part of the string data:image/jpeg;base64,, (of course its not necessarily a jpeg.. its just an example). I decoded the rest of the string back to an image, and passed the content_type also.

That solved my problem

Upvotes: 2

Related Questions