How to upload a file to s3 from a file upload form after modifying it with PIL? How to upload a file to s3 from a file upload form after modifying it with PIL? flask flask

How to upload a file to s3 from a file upload form after modifying it with PIL?


For proper debugging purposes you really need to look at a traceback. It will tell you what is wrong :-)!!

Otherwise: I guess your general approach (first create an image file in the file system and then use boto for uploading it) is fine. However, for debugging purposes you could check what img.save('out.png',"PNG") leaves behind. You can, for investigation purposes, test if the file exists and otherwise raise an exception: assert os.path.isfile("out.png"). Also, you may want to print the file size using os.path.getsize("out.png"). As far as I remember working with boto, k.set_contents_from_filename("out.png") is the right thing to do then.

That is, your order of doing things is right. Most probably there is an authentication/connection problem with S3, as Dmitry already pointed out. The details of this problem you will find by looking at a Traceback. A boto traceback will contain the error AWS error response.


Please, use the trick below to see all raw http requests that boto sends to S3:

import httplibhttplib.HTTPConnection.debuglevel = 1

also you could use this hint for the same:

import logginglogging.basicConfig(filename="boto.log", level=logging.DEBUG)

Before testing functionality using web server please try to execute some test code from default python console:

import httplibhttplib.HTTPConnection.debuglevel = 1conn = boto.connect_s3(aws_access_key_id='some', aws_secret_access_key='some')b = conn.get_bucket('snappie.watermarks')k = Key(b)k.key = "test.txt"k.set_contents_from_string('12345')

After this please check file existence. In any case after all manipulations you should execute:

key.make_public()

Because by default all new bucket objects aren't public.


Basically, I think the server error was formed when there was no "image" object in the request.files dictionary yet the request was sent. Therefore the server had no idea what to do. I fixed it by using request.files.get('image') instead of request.files['image']. Thus if there is no image, it returns None instead of giving a key error.

There was still no trackback, though I know everyone wanted one, because this kind of server error in flask DOES NOT seem to return a traceback. Simply a "Bad request" error message in the browser.