I'm trying to figure how to accept images via a rest api. If my api does something like this:
def post(self):
sent_file = next(request.files.values())
sent_file.seek(0)
file_data = sent_file.stream.read()
import boto
from boto.s3.key import Key
....
k = Key(bucket)
k.set_contents_from_string(file_data)
k.make_public()
and the request I send is:
rv = s.put('.../fileupload', files={'test.jpg': open('test.jpg', 'rb')})
this works but I know there is a cleaner solution. How do I send a file so that I can upload the file itself and not generate a string of the data and upload that? Also, is there an easy way to ensure the file is an image?
First of all I would recommend using postman to test RestFUL API's. You are getting a string because you are opening the file with put which forcibly produces a byte stream You can upload files using form-data with post. Unfortunately HTTP cannot type check file extension or file types. Better to check that at client or at server.