Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Expires headers for amazon s3 images?

In my model, which is using paperclip. I have added this line of code but still its not working

 has_attached_file :attachment, {
    :styles => {
      :medium => ["654x5000000>", :jpg],
      :small => ["260x50000000>", :jpg], 
      :thumb => ["75x75#", :jpg],
      :facebook_meta_tag =>["200x200#", :jpg] 
    },
    :convert_options => {
       :medium => "-quality 80 -interlace Plane",
       :small => "-quality 80 -interlace Plane",
       :thumb => "-quality 80 -interlace Plane",
       :facebook_meta_tag => "-quality 80 -interlace Plane" 
       },
       :s3_headers => { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate } 
    }.merge(PAPERCLIP_STORAGE_OPTIONS)

PS: I tested it on GTmetrix.com and as per their stats, expiry headers are not there in amazon images.

like image 510
Mohit Jain Avatar asked Oct 21 '12 00:10

Mohit Jain


2 Answers

To expand on James' answer for others unsure about how to reprocess your old attachments, there are two ways to do so:

1) Reprocess all attachments via rake (optionally per CLASS)

rake paperclip:refresh CLASS=User

2) Reprocess specific attachments (e.g. via migration)

User.where("attachment IS NOT NULL").find_each do |user|
  user.attachment.reprocess!
end

For more info, see https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation#generatingregenerating-your-thumbnails

like image 198
mwalsher Avatar answered Oct 12 '22 23:10

mwalsher


It looks like you didn't re-process/upload your old attachments and that's why their headers haven't changed.

like image 36
James Avatar answered Oct 12 '22 23:10

James