Devarshi Goswami
Devarshi Goswami

Reputation: 1225

python aws botocore.response.streamingbody to json

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?

Upvotes: 11

Views: 24176

Answers (2)

anantha ramu
anantha ramu

Reputation: 171

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)

Upvotes: 0

Marcin
Marcin

Reputation: 238827

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

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

Upvotes: 20

Related Questions