使用 Python boto3 从 S3 读取 JSON 文件 [英] Reading an JSON file from S3 using Python boto3
本文介绍了使用 Python boto3 从 S3 读取 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在 S3 存储桶测试"中关注 JSON
I kept following JSON in S3 bucket 'test'
{
'Details' : "Something"
}
我正在使用以下代码读取此 JSON 并打印密钥详细信息"
I am using following code to read this JSON and printing the key 'Details'
s3 = boto3.resource('s3',
aws_access_key_id=<access_key>,
aws_secret_access_key=<secret_key>
)
content_object = s3.Object('test', 'sample_json.txt')
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(repr(file_content))
print(json_content['Details'])
我收到错误,因为'字符串索引必须是整数'我不想从 S3 下载文件然后阅读..
And i am getting error as 'string indices must be integers' I don't want to download the file from S3 and then reading..
推荐答案
正如上面评论中提到的,repr
必须被删除,json
文件必须使用双引号用于属性.在 aws/s3 上使用此文件:
As mentioned in the comments above, repr
has to be removed and the json
file has to use double quotes for attributes. Using this file on aws/s3:
{
"Details" : "Something"
}
和以下 Python 代码,它可以工作:
and the following Python code, it works:
import boto3
import json
s3 = boto3.resource('s3')
content_object = s3.Object('test', 'sample_json.txt')
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
print(json_content['Details'])
# >> Something
这篇关于使用 Python boto3 从 S3 读取 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文