Skip to content

Instantly share code, notes, and snippets.

@BrianSigafoos
Forked from rahul100885/s3_header.rb
Last active August 29, 2015 14:24
Show Gist options
  • Save BrianSigafoos/b9e9a860bf441dad1b6b to your computer and use it in GitHub Desktop.
Save BrianSigafoos/b9e9a860bf441dad1b6b to your computer and use it in GitHub Desktop.
AWS S3 Cache Control Max-Age
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
s3 = AWS::S3.new(
:access_key_id => 'PASTE_HERE',
#### SECRET KEY #####
:secret_access_key => 'PASTE_SECRET_KEY_HERE')
#### DO NOT COMMIT ####
bucket = s3.buckets['my-bucket-name']
ten_year_in_seconds = 10 * 365 * 24 * 60 * 60
ten_year_from_now = Time.now + ten_year_in_seconds
fifteen_days = 15 * 24 * 60 * 60
# to update an existing object
bucket.objects.with_prefix('production/defaults/foldername').each do |obj|
begin
obj.copy_to(obj.key,
:acl => :public_read,
:content_type => "image/jpeg",
:cache_control => "max-age=#{fifteen_days}")
rescue Exception => e
puts "Exception #{e}"
end
end
# see more http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html
# reference: http://www.thehorrors.org.uk/snippets/aws-ruby-s3-set-content-expires-data/
# forked from: https://gist.github.com/rahul100885/d788a041e2c36fd40f24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment