从 S3 中读取带有 Python 的 json 文件到 sagemaker notebook

read json file with Python from S3 into sagemaker notebook

我想将 json 文件从 S3 读取到 sagemaker 笔记本中。

我可以用 pandas 和这段代码做到这一点,而且没有错误:

import json
import pandas as pd
import boto3


prefix_source = 'folder'

s3 = boto3.resource('s3')
my_bucket_source = s3.Bucket('bucket_source')

for obj in my_bucket_source.objects.filter(Prefix=prefix_source):
        data_location = 's3://{}/{}'.format(obj.bucket_name, obj.key)
        data = pd.read_json(data_location, lines = True )
        display(data.head())

但是我不想用pandas,我想用Python

我试过这个代码

for obj in my_bucket_source.objects.filter(Prefix=prefix_source):
        data_location = 's3://{}/{}'.format(obj.bucket_name, obj.key)
        with open(data_location, 'r') as f:
            array = json.load(f)
            display(array) 

我遇到了这个错误:

IOError: [Errno 2] 没有那个文件或目录

Json.load() 期望本地文件系统路径“/...”,而不是 "s3://" URI。
在这里查看答案: