I am using Chalice to build a simple severless application which returns an image file from S3.
I am able to return file after it is 64 bit encoded. But I am wondering how I can return the binary file so that user can take it as a file download? My following code is not working.
@app.route('/binary_object/{bucket}/{key}', methods=['GET', 'PUT'])
def binary_object(bucket, key):
request = app.current_request
if request.method == 'GET':
try:
file_path = '/tmp/{}_{}'.format(uuid.uuid4(), key)
s3_client.download_file(bucket, key, file_path)
file_size = os.path.getsize(file_path)
headers = {'Content-Disposition': 'attachment; filename=\"' + key + '\"',
'Content-Type': 'application/octet-stream',
# 'Content-Type': 'image/*',
'Content-Length': str(file_size)}
fsk = open(file_path, 'rb')
return Response(body=fsk, headers=headers, status_code=200)
except Exception as e:
print e
raise e