我是 python 新手,按照现有帖子的答案,我尝试从 amazon S3 读取 json 文件,如下所示:
import boto3
import os
BUCKET = 'my_bucket'
FILE_TO_READ = 'file.json'
client = boto3.client('s3',
aws_access_key_id=os.environ.my_key,
aws_secret_access_key=os.environ.my_secret_key
)
result = client.get_object(Bucket=BUCKET, Key='file')
text = result["Body"].read().decode()
print(text['key']) # Use your desired JSON Key for your value
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?
该错误表明您的密钥不存在。我看到您已经拥有密钥“file.json”,所以我假设它存在。我假设您的文件是这样存储的bucket/same_file/file.json。如果是这种情况,请尝试以下操作:
import json
import boto3
s3_obj =boto3.client('s3')
s3_clientobj = s3_obj.get_object(Bucket='your_bucket', Key='file/file.json')
s3_clientdata = s3_clientobj['Body'].read().decode('utf-8')
print("printing s3_clientdata")
print(s3_clientdata)
Run Code Online (Sandbox Code Playgroud)
如果你想做数据操作,一个更Pythonic的解决方案是:
fs = s3fs.S3FileSystem()
with fs.open('yourbucket/file/your_json_file.json', 'rb') as f:
s3_clientdata = json.load(f)
#Convert to df
df = pd.DataFrame(s3_clientdata,index=[0])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5815 次 |
| 最近记录: |