How can I use boto to stream a file out of Amazon S3 to Rackspace Cloudfiles?

后端 未结 5 1010
清歌不尽
清歌不尽 2020-11-30 00:14

I\'m copying a file from S3 to Cloudfiles, and I would like to avoid writing the file to disk. The Python-Cloudfiles library has an object.stream() call that looks to be wh

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 00:44

    This is my solution of wrapping streaming body:

    import io
    class S3ObjectInterator(io.RawIOBase):
        def __init__(self, bucket, key):
            """Initialize with S3 bucket and key names"""
            self.s3c = boto3.client('s3')
            self.obj_stream = self.s3c.get_object(Bucket=bucket, Key=key)['Body']
    
        def read(self, n=-1):
            """Read from the stream"""
            return self.obj_stream.read() if n == -1 else self.obj_stream.read(n)
    

    Example usage:

    obj_stream = S3ObjectInterator(bucket, key)
    for line in obj_stream:
        print line
    

提交回复
热议问题