Search code examples
python-2.7encodinggoogle-cloud-platformgoogle-cloud-storagegoogle-cloud-vision

What encoding does blob.download_as_string() return?


I am downloading a file from Google Storage as a byte string, b64 encoding it, and using that as input into the Google Vision API.

storage_client = storage.Client(project=[PROJECT])
bucket = storage_client.get_bucket([BUCKET])
blob = bucket.blob([KEY])

content = blob.download_as_string()
b64content = base64.b64encode(content)

client = vision.ImageAnnotatorClient()
image = vision.types.Image(content=b64content)

I am getting a bad image error using the b64content. However, if I use the non base64 content, my call to the Vision API succeeds:

image = vision.types.Image(content=content)

Does blob.download_as_string() return a byte string that is already base64 encoded?


Solution

  • Short answer: no, it is not base64 encoded. Then why does it work with the non-encoded string?

    Using the Python Client as you do, you don't need to encode the string, as seen here. You need to encode it if you post a Vision API request in JSON, like this one. This is why you get it working already without base64.b64encode().