Read a file line by line from S3 using boto?

前端 未结 10 1030
刺人心
刺人心 2020-11-29 07:32

I have a csv file in S3 and I\'m trying to read the header line to get the size (these files are created by our users so they could be almost any size). Is there a way to do

10条回答
  •  迷失自我
    2020-11-29 08:12

    Here's a solution which actually streams the data line by line:

    from io import TextIOWrapper
    from gzip import GzipFile
    ...
    
    # get StreamingBody from botocore.response
    response = s3.get_object(Bucket=bucket, Key=key)
    # if gzipped
    gzipped = GzipFile(None, 'rb', fileobj=response['Body'])
    data = TextIOWrapper(gzipped)
    
    for line in data:
        # process line
    

提交回复
热议问题