python将botocore.response.streamingbody转换为json



我正在使用boto3访问S3中的文件,目标是读取文件并将其转换为JSON但问题是没有一个文件有任何文件扩展名(没有.csv、.json等(,尽管文件中的数据结构类似于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']返回一个<botocore.response.StreamingBody>对象

有可能找出里面的数据吗?

扩展无关紧要。假设你的文件包含有效的json,你可以得到它:

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

响应是一个字典对象。Response在"Body"属性中返回StreamingBody。这就是解决方案。

点击此处查找更多信息。Boto S3获取对象

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)

最新更新