python aws botocore.response.streamingbody to json

Viewed 3903

I am using boto3 to acccess files from S3, The objective is to read the files and convert it to JSON But the issue is none of the files have any file extension (no .csv,.json etc),although the data in the file is structured like JSON

client = boto3.client(
    's3',
    aws_access_key_id = 'AKEY',
    aws_secret_access_key = 'ASAKEY',
    region_name = 'us-east-1'
)
obj = client.get_object(
    Bucket = 'bucketname',
    Key = '*filename without extension*'
)

obj['Body'] returns a <botocore.response.StreamingBody> object

is it possible to find out the data within it?

2 Answers

The extension does not matter. Assuming your file contains valid json, you can get it:

my_json = json.loads(obj['Body'].read())

The response is a dictionary object. Response returns StreamingBody in 'Body' attribute. So here is the solution.

Find more information here. Boto S3 Get Object

    client = boto3.client('s3')
    response = client.get_object(
        Bucket='<<bucket_name_here>>',
        Key='<<file key from aws mangement console (S3 Info) >>'
    )
    jsonContent = json.loads(response['Body'].read())

    print(jsonContent)
Related