我正在使用烧瓶 - 不安。我试图回应来自s3的json对象的gz文件的get请求,并且不了解如何发送gz文件。
class report_download(Resource):
def get(self,report_name):
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(bucketname)
key = Key(bucket, report_name)
key.get_contents_to_filename('/tmp/%s' % report_name)
os.system('gzip /tmp/%s' % report_name)
data = open('/tmp/%s.gz' % report_name).readlines()[0]
return data
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/socket.py", line 468, in sendall
data_sent += self.send(_get_memory(data, data_sent), flags)
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/socket.py", line 439, in send
return sock.send(data, flags)
error: [Errno 32] Broken pipe
127.0.0.1 - - [26/May/2016 03:29:03] "GET /api/driver/report/download/7000_upload_.json HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 481, in wrapper
return self.make_response(data, code, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/__init__.py", line 510, in make_response
resp = self.representations[mediatype](data, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Flask_RESTful-0.3.5-py2.7.egg/flask_restful/representations/json.py", line 20, in output_json
dumped = dumps(data, **settings) + "\n"
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte
答案 0 :(得分:1)
压缩数据是二进制的,因此,readlines
可能只会读取损坏的数据。
您可以使用python模块gzip
,而无需任何外部程序:
import gzip
class report_download(Resource):
def get(self,report_name):
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(bucketname)
key = Key(bucket, report_name)
contents = key.get_contents_as_string()
data = StringIO.StringIO()
zip = gzip.GzipFile(fileobj=data, mode='wb')
zip.write(contents)
zip.close()
return Response(data.getvalue(), mimetype='application/x-gzip')