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

时间:2019-05-29 20:07:28

标签: python-3.x amazon-web-services amazon-s3 aws-lambda boto3

在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.

失败的代码区域在这里:

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)

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我猜这个问题是由于流的大小而引起的。我用 _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)