Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从S3读取JSON_Python_Amazon S3 - Fatal编程技术网

Python 从S3读取JSON

Python 从S3读取JSON,python,amazon-s3,Python,Amazon S3,我是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,

我是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  
但是,我收到以下错误:

NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

有人能告诉我我做错了什么吗?

我看到您正在用中的键“file”检索对象

我想你的意思是:

result = client.get_object(Bucket=BUCKET, Key=FILE_TO_READ)

有关更多详细信息,请参阅。

错误表示您的密钥不存在。我看到您已经有了密钥“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)
如果您想进行数据管理,一个更具python风格的解决方案是:

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])
错误表示“指定的密钥不存在”。尝试:Amazon console>S3>Your bucket>单击要下载的文件>概览>键。你应该试着用那把钥匙
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])