烧瓶:从S3返回图像

时间:2020-05-12 18:35:18

标签: python python-3.x rest flask flask-restful

我试图以附件形式返回图像,但是在make_response()调用期间出现错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

get API的定义定义为:

class FileDownload(Files):
    def get(self):
        ...
        headers = {}
        headers['Content-Type'] = file.mimetype
        headers['Content-Disposition'] = 'attachment;filename='+file.name
        return {'bucket': bucket, 'file': s3path}, 200, headers

以下是text / html的定义:

@api.representation('text/html')
def output_image(data, code, headers=None):
    ...
    obj = s3.Object(bucket, data['file'])
    data = obj.get()
    body = data['Body'].read()
    fileobj = io.BytesIO(body)
    resp = make_response(fileobj.read())
    resp.headers.extend(headers or {})
    return resp
api.add_resource(FileDownload, '/download')

0 个答案:

没有答案