POST upload support is a fairly new feature of S3. It was proposed in November 2007 and released for beta in December 2007. The documention is here.
This article documents some of the lessons I learned when integrating SWFUpload with S3. I’ve not got it working perfectly yet, but I’m very nearly there.
First, some background. Many sites already use S3 to store uploaded files, but until now this had required a two stage process:
- The user has to wait for the file to upload to the site, and then for it to be copied over to S3. So when using an upload component with a progress indicator there will be an additional delay even after it reaches 100%.
- Handling uploads in your app puts extra demand on your webserver
Direct uploads to S3 require a number of extra fields to be sent with the request. This is only supported in Flash 9. You cannot upload to S3 with Flash 8 or below.
Now, on to the implementation. Rather than trying to integrate everything at once, I recommend you take small steps:
First, get S3 uploads work using a standard file input button. Use this form generator to crytographically sign your upload policy, which gets sent with the request, and create your HTML. Note that whenever you make a chnage to the fields that get sent with your form, you’ll need to re-sign it.
Next, get SWFUpload working with a non-S3 site by studying the simple upload example. This will involve download all the necessary supporting files and making sure they’re in the right place.
Then there’s the fun try to combine the two. Here’s some important lessons I learned:
- SWFUpload has a debug option but it does not display error pages returned by S3, only the status code. To see the error page, use a HTTP analyser such as Charles.
- Host your swfupload.swf on your S3 account, then you don’t need to worry about crossdomain.xml
- S3 expects the file post name to be “File” rather than SWFUpload’s default of “Filedata”. You can change this using the file_post_name parameter.
Now, here’s the reason why I’m almost there but not quite. S3 allows to your set the response code for a successful upload to be 200, 201 or 204. But SWFUpload considers 201 and 204 to be error responses. So that leaves 200. But it doesn’t like responses with an empty body, which 200 returns, so it will hang at 100% even though the upload is successful. So what do you do?
- Be patient and wait for the new release of SWFUpload which should fix this
- Compile your own version of SWFUpload to circumvent this as described here.
I plan to do the latter, and will share my modified SWFUpload here when done.