Read a file line by line from S3 using boto?

前端 未结 10 1040
刺人心
刺人心 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:03

    The codecs module in the stdlib provides a simple way to encode a stream of bytes into a stream of text and provides a generator to retrieve this text line-by-line. It can be used with S3 without much hassle:

    import codecs
    
    import boto3
    
    
    s3 = boto3.resource("s3")
    s3_object = s3.Object('my-bucket', 'a/b/c.txt')
    line_stream = codecs.getreader("utf-8")
    
    for line in line_stream(s3_object.get()['Body']):
        print(line)
    

提交回复
热议问题