简体   繁体   English

如何为amazon s3图像添加Expires标头?

[英]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. PS:我在GTmetrix.com上进行了测试,根据他们的统计数据,亚马逊图像中没有到期标题。

To expand on James' answer for others unsure about how to reprocess your old attachments, there are two ways to do so: 为了扩展James对其他人不确定如何重新处理旧附件的回答,有两种方法可以做到:

1) Reprocess all attachments via rake (optionally per CLASS) 1)通过rake重新处理所有附件(可选择每个CLASS)

rake paperclip:refresh CLASS=User

2) Reprocess specific attachments (eg via migration) 2)重新处理特定附件(例如通过迁移)

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 有关详细信息,请参阅https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation#generatingregenerating-your-thumbnails

看起来您没有重新处理/上传旧附件,这就是为什么他们的标题没有改变的原因。

如果有人需要使用动态过期标题获取附件的URL,这对我有用:来自paperclip文档链接的 attachment.expiring_url(60 * 60)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM