从 AWS Lambda 上的 S3 读取文件时出现 IncompleteReadError

Aus*_*tin 6 amazon-s3 amazon-web-services python-3.x boto3 aws-lambda

当从 AWS Lambda 上的 S3 读取文件时,我得到IncompleteReadError. 当我在本地尝试时,效果很好。这仅发生在 Python3.6 上,并且在 Python3.7 上运行良好 - 但是我需要使用 Python3.6。我也尝试使用资源而不是客户端,但得到了相同的错误

Traceback (most recent call last):
  File "/var/task/function.py", line 141, in handler
    i = d.read()
  File "/var/runtime/botocore/response.py", line 82, in read
    self._verify_content_length()
  File "/var/runtime/botocore/response.py", line 134, in _verify_content_length
    expected_bytes=int(self._content_length))
botocore.exceptions.IncompleteReadError: 0 read, but total bytes expected is 36678.
Run Code Online (Sandbox Code Playgroud)

失败的代码区域在这里:

client = boto3.client('s3')        
get_json_file = client.get_object(
    Bucket=os.environ['S3_BUCKET'],
    Key="{0}".format(file_name),
)

d = get_json_file.get('Body')
i = d.read()
data = json.loads(i)
Run Code Online (Sandbox Code Playgroud)

小智 5

我遇到了同样的问题,我猜问题是由于流的尺寸太大而出现的。我曾经_raw_stream解决过这个问题

尝试这样做:

client = boto3.client('s3')        
object = client.get_object(
    Bucket=os.environ['S3_BUCKET'],
    Key="{0}".format(file_name),
)

raw_data = object['Body']._raw_stream.data.decode("utf-8")
data = json.loads(raw_data)
Run Code Online (Sandbox Code Playgroud)