Alex
Alex

Reputation: 5214

Multipart passthrough in API Gateway

I have a web application that accepts multipart/form-data and have set up api gateway to pass through the requests to it. However, any time I upload images as part of the form they end up being larger than the original file I uploaded and corrupted. If I execute the same method without going via AWS API gateway the image uploads fine. Any ideas what I'm missing here?

the below is my S3 code that reads the form data in flask:

    f = request.files['attachment']
    fp = StringIO(f.read())
    file_uuid = str(uuid.uuid4())
    bucketkey.key = "att_%s%s" % (file_uuid, file_extension(f.filename))
    bucketkey.set_contents_from_file(fp)

I'll attach a link to a sample jpg that is corrupted if its useful at all.

https://s3.eu-central-1.amazonaws.com/agendacommunities/att_ef7375b1-bb19-4df9-9162-5582ed66797c.jpeg

Upvotes: 1

Views: 867

Answers (2)

Vitalii Budkevych
Vitalii Budkevych

Reputation: 217

You need to set content-type header for your data to be recognized as binary by API Gateway.

Upvotes: 0

Kevin Schellenberg
Kevin Schellenberg

Reputation: 845

What is your intent with StringIO? f.read() will give you bytes, not a string. I think you can pass f directly to set_contents_from_file and it will read from f.

f = request.files['attachment']
file_uuid = str(uuid.uuid4())
bucketkey.key = "att_%s%s" % (file_uuid, file_extension(f.filename))
bucketkey.set_contents_from_file(f)

Upvotes: 1

Related Questions