The HW Blog

Want to be updated? @opencoconut | Feed

Uploading videos directly to Amazon S3

Bruno Celeste (@brunoceleste)

April 01, 2014 Tech

It's pretty straightforward thanks to Amazon, you can now save some bandwidth and allow your user to upload directly to your S3 bucket.

Here is a very simple example:

<form action="http://yourbucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="<%= current_user.id %>/videos/<%= random_key %>" /><br />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
<input type="hidden" name="redirect" value="http://yourbucket.s3.amazonaws.com/upload_ok?user_id=<%= current_user.id %>" >
<input type="hidden" name="AWSAccessKeyId " value="15B4D3461F177624206A" />
<input type="hidden" name="Policy" value="eyAiZXhwaXJhdGlvbiI6ICIy..." />
<input type="hidden" name="Signature" value="2qCp0odXe7A9IYyUVqn0w2adtCA=" />
Video file <input type="file" name="file" /> <br />
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>

When an user upload the file to your bucket, the response will be:

HTTP/1.1 303 Redirect
x-amz-request-id: 1AEE782442F35865
x-amz-id-2: cxzFLJRatFHy+NGtaDFRR8YvI9BHmgLxjvJzNiGGICARZ/mVXHj7T+qQKhdpzHFh
Content-Type: application/xml
Date: Wed, 14 Nov 2007 21:21:33 GMT
Connection: close
Location: http://yourbucket.s3.amazonaws.com/upload_ok?user_id=39923&bucket=yourbucket&key=39923/videos/pcSd1r8e6lk&etag=39d459dfbc0faabbb5e179358dfb94c3
Server: AmazonS3

In upload_ok page, you'll makethe request to Coconut to transfer and encode the video:

user = User.find(params[:user_id])
url = "http://yourbucket.s3.amazonaws.com/#{params[:key]}"

hw = Coconut.new("HW-API-Key", api_key)
hw.download :create :url => url
...

See the S3 documentation.



Any Comment? Send us a message on Twitter @opencoconut
If you have questions or feedbacks, you can also contact us via our contact page