Search code examples
amazon-s3paperclipfog

How to set the Content Disposition header with Paperclip / fog for a file hosted on S3?


I'm using Paperclip 4.2.0 and fog 1.24.0, and host files on S3. I want to generate an expiring URL that has the "Content-Disposition" header set to "attachment".

Paperclip has this option to pass additional parameters to S3 expiring URLs but I can't have it working when using Paperclip with Paperclip::Storage::Fog.

This fog issue gives the following solution:

file.url(60.seconds.from_now, { :query => { 'response-content-disposition' => 'attachment' } }

but it does not work for me. My Rails model ResourceDocument has has_attached_file :target. document.target.url(60.seconds.from_now, { :query => { 'response-content-disposition' => 'attachment' } } returns the same URL than document.target.url(60.seconds.from_now), ie no content-disposition is included in the generated URL: "xxx.s3.amazonaws.com/uploads/resource_documents/targets/40/2014-12-01%2017:26:20%20UTC/my_file.csv"


Solution

  • I am using aws-sdk gems and it works fine for me, hope this helpful for you.

    gem 'aws-sdk-core'
    gem 'aws-sdk'
    

    and model's method:

    def download_url
      s3 = AWS::S3.new
      s3_videos_bucket = 'xxxx' #bucket name goes right here
      bucket = s3.buckets[s3_videos_bucket]
      object_path = 'xxxx' #file path goes right here
      object = bucket.objects[object_path]
      object.url_for(:get, { 
        expires: 60.minutes,
        response_content_disposition: 'attachment;'
      }).to_s
    end