Policia

Information

Policia is a simple generator of Amazon S3 policy document signatures for use in authenticating POST/REST/API uploads to you S3 buckets.

For more information on Amazon S3 Policy Documents see the their documentation.

Installation

Install the latest stable release:

[sudo] gem install policia

Or if using bundler add the following to your Gemfile:

gem 'policia'

Example use

An quick example, note to at least change YOUR-S3-BUCKET-NAME, YOUR-S3-ACCESS-KEY and YOUR-S3-SECRET-KEY

@document = { "expiration" => "2012-12-12T00:00:00Z",
              "conditions" => [ {"bucket" => "YOUR-S3-BUCKET-NAME"},
                                ["starts-with", "$key", ""],
                                {"acl" => "public-read"},
                                {"success_action_redirect" => "YOUR-CHOICE-OF-CALLBACK"},
                                ["starts-with", "$Content-Type", "image"] ] }

@policy_document = Policia.new("YOUR-S3-SECRET-KEY", @document)

Then in your form (i.e. using ERB template)

<form action="http://YOUR-S3-BUCKET-NAME.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
  <input type="hidden" name="key" value="${filename}">
  <input type="hidden" name="AWSAccessKeyId" value="YOUR-S3-ACCESS-KEY">
  <input type="hidden" name="acl" value="public-read">
  <input type="hidden" name="success_action_redirect" value="YOUR-CHOICE-OF-CALLBACK">

  <input type="hidden" name="policy" value="<%= @policy_document.policy %>">
  <input type="hidden" name="signature" value="<%= @policy_document.signature %>">

  <input type="hidden" name="Content-Type" value="image/jpeg">
  <!-- Include any additional input fields here -->

  File to upload to S3: <input name="file" type="file"> <input type="submit">
</form>

The example above will post your file to your amazon s3 bucket root and then return to your given callback with parameters containing file info.