Created
September 12, 2010 06:41
-
-
Save tfe/575907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CDN-ify Assets | |
# | |
# This script will traverse all the objects in an S3 bucket and set ACLs and | |
# cache headers as appropriate for CDN (CloudFront) distribution. Specifically, | |
# it sets them to expire 10 years from now (far-future expiration) and ACLs to | |
# be publicly readable. | |
# | |
# Use this if you have an S3 bucket full of assets that didn't get proper settings | |
# set when you uploaded them, and you need an easy way to fix them now. | |
# | |
# Feel free to run this multiple times against the same bucket. It sets metadata | |
# on the CDN-ified objects and will not re-cdn-ify objects that have already been | |
# done. | |
# | |
# Usage: | |
# ruby cdnify_assets.rb my_aws_access_key my_aws_secret_access_key my_fancy_bucket | |
require 'rubygems' | |
require 'aws/s3' | |
STDOUT.sync = true | |
access_key_id, secret_access_key, bucket_name = ARGV | |
c = AWS::S3::Base.establish_connection!( | |
:access_key_id => access_key_id, | |
:secret_access_key => secret_access_key | |
) | |
bucket = AWS::S3::Bucket.find(bucket_name) | |
puts "Found bucket '#{bucket_name}'" | |
puts "#{bucket.objects.size} objects found" | |
if bucket.objects.size > 0 | |
puts "Updating..." | |
bucket.objects.each do |o| | |
if o.metadata[:cdnified_at] | |
print '.' | |
else | |
o.metadata[:cdnified_at] = Time.now.httpdate | |
o.cache_control = 'max-age=315360000, public' | |
o.expires = (Time.now + 315360000).httpdate | |
o.save({:access => :public_read}) | |
print '+' | |
end | |
end | |
end | |
puts "\nFinished" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment