Skip to content
Browse files

8 years expires headers on all s3 uploads

  • Loading branch information...
1 parent b1cc8ce commit cbd317877a0c038d40114a7c02a0a2f6f733e6bb @sudara sudara committed Mar 11, 2009
Showing with 19 additions and 0 deletions.
  1. +1 −0 config/initializers/setup.rb
  2. +18 −0 lib/asset_hacks.rb
View
1 config/initializers/setup.rb
@@ -15,6 +15,7 @@
require 'randomness'
require 'goodies'
require 'utils'
+require 'asset_hacks'
# require s3 and the s3 expires hack
#require 'aws/s3'
View
18 lib/asset_hacks.rb
@@ -0,0 +1,18 @@
+require 'aws/s3'
+
+# Adds expiration headers to all stored S3 objects through duck-punching.
+# Based on Keaka Jackson's original work.
+# Sets cache_control and expires headers on all uploads
+module AWS::S3
+ class S3Object
+ class << self
+ MAX_AGE = 8.years
+ def store_with_cache_control(key, data, bucket = nil, options = {})
+ options[:cache_control] = "max-age=#{MAX_AGE.to_i}" unless options[:cache_control]
+ options[:expires] = MAX_AGE.from_now.httpdate unless options[:expires]
+ store_without_cache_control(key, data, bucket, options)
+ end
+ alias_method_chain :store, :cache_control
+ end
+ end
+end

0 comments on commit cbd3178

Please sign in to comment.
Something went wrong with that request. Please try again.