Open S3 object as a string with Boto3

Gahl Levy picture Gahl Levy · Aug 13, 2015 · Viewed 191k times · Source

I'm aware that with Boto 2 it's possible to open an S3 object as a string with: get_contents_as_string()

Is there an equivalent function in boto3 ?

Answer

Kamil Sindi picture Kamil Sindi · Feb 13, 2016

read will return bytes. At least for Python 3, if you want to return a string, you have to decode using the right encoding:

import boto3

s3 = boto3.resource('s3')

obj = s3.Object(bucket, key)
obj.get()['Body'].read().decode('utf-8')