【问题标题】:IncompleteReadError when reading file from S3 on AWS Lambda从 AWS Lambda 上的 S3 读取文件时出现 IncompleteReadError
【发布时间】:2019-10-15 11:48:31
【问题描述】:

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

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多