S3 file to local using luigi raises UnicodeDecodeError

泪湿孤枕 提交于 2019-12-07 06:09:17
Alexey Grigorev

It seems that you're dealing with a binary file as if it was text - but it's not. You probably need to do something like this:

with self.input().open('r') as i, self.output().open('w') as o:
    o.write(i.read())

(not tested!)

Also, I think you may find this answer useful: Python writing binary files, bytes

I ended using luigi.s3.S3Clientand the method get which copies a binary file from/to the s3.

Snippet of code:

import luigi

class ATask(luigi.Task):
    s3_path = luigi.Parameter()
    local_path = luigi.Parameter()
    ...

    def run(self):
        client = luigi.s3.S3Client()
        client.get(s3_path, local_path)  ## This gets the file
        ...

I think that the underlying reason is that luigi uses boto for getting/putting files from/to the s3. (As you can see in the source code)

Another option would be to use the format kwarg luigi.format.Nop to Target to indicate that it is a binary file that is being read.

e.g.

def output(self):
    return LocalTarget(target, format=luigi.format.Nop)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!