I have a zip file in S3 that I'm trying to return to the user. The end result is that when the user hits my API it should download this zip file on the user's computer.
Here's how I got the data from S3 (the data can also be retrieved by using s3.get_object)
@pyramid_view.view_config(route_name='route_name', renderer='string')
async def get_data(request):
res = await(func(request))
return res
async def func(request):
url = s3_client.generate_presigned_url('get_object',
Params={'Bucket': DEFAULT_BUCKET_NAME,
'Key': path},
ExpiresIn=3600)
response = urllib.request.urlopen(url)
finalData = response.read()
return finalData
Once I get finalData
, I set the headers as
request.response.content_type = 'binary/octet-stream'
request.response.headers['Access-Control-Allow-Methods'] = 'POST, GET'
request.response.headers['Access-Control-Expose-Headers'] = 'X-filename'
request.response.headers['Content-Transfer-Encoding']='binary'
request.response.headers['X-filename'] = path
request.response.content_disposition = 'attachment;filename=try.zip'
and then return finalData
from my API.
The file gets downloaded however when I try opening it, it says:
Does anyone have any clue why it might be happening? It seems it works fine for a flask app. I'm trying to make this work in an API gateway.
I tried to read the file that I downloaded via a hex editor and it shows this
If I do responseFinal.decode(encoding="utf-8", errors="ignore")
, the file downloaded now looks like this(still doesnt get unzipped).
The file that got downloaded successfully via presigned url in a hex editor looks like. Is there some encoding that I'm missing? (I have tried utf-8 decoding but that fails for the zip file)