How to add additional S3 metadata? #32

rahilsondhi opened this Issue Dec 22, 2012 · 5 comments

4 participants


I'd like to add Content-Disposition: attachment to all uploaded files so that if the download link is visited, browsers are forced to download the file.

For example if you hit the download URL for an mp3 without Content-Disposition: attachment, Chrome will just stream the mp3 in the browser. But if you set Content-Disposition: attachment, Chrome will download the file.



Also interested. Did you sort this out?


A quick glance at the s3 doc shows that you can pass a "Content-Disposition" field with direct uploads. So you may be able to add/remove this field from the form at runtime by inspecting the filename of the uploaded file. Perhaps in the jquery file upload "add" callback?


Okay got it working.

In the Rails s3_uploader_form I added <%= hidden_field_tag 'Content-Disposition', 'attachment' %>

Then in config/initializers/s3_direct_upload.rb, I monkey patched the policy_data method. After the Content-Type line I added ["starts-with","$Content-Disposition",""],

Note that this means S3 will always expect the Content-Disposition field on every request. If you remove the hidden field, S3 will reject your requests because you're not providing all the fields you promised in the policy.

This isn't the best solution, but it solves my problem for now.

Here is my full initializer:

S3DirectUpload.config do |c|
  c.access_key_id =
  c.secret_access_key =
  c.bucket =

module S3DirectUpload
  module UploadHelper
    class S3Uploader
      def policy_data
          expiration: @options[:expiration],
          conditions: [
            ["starts-with", "$utf8", ""],
            ["starts-with", "$key", ""],
            ["starts-with", "$x-requested-with", ""],
            ["content-length-range", 0, @options[:max_file_size]],
            {bucket: @options[:bucket]},
            {acl: @options[:acl]},
            {success_action_status: "201"}

Thanks! Worked for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment