Skip to content

Instantly share code, notes, and snippets.

@tomriley
Forked from rahul100885/s3_header.rb
Created September 7, 2017 09:08
Show Gist options
  • Save tomriley/a18ae5220944cf7806178efda0006740 to your computer and use it in GitHub Desktop.
Save tomriley/a18ae5220944cf7806178efda0006740 to your computer and use it in GitHub Desktop.
Ruby script to add Metadata ( Cache Control and Expires) header to existing S3 Objects
require 'rubygems'
require 'aws-sdk'
s3 = AWS::S3.new(
:access_key_id => 'XXXXXXXXXXXXXXX',
:secret_access_key => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
bucket = s3.buckets['your_bucket_name']
ten_year_in_seconds = 10 * 365 * 24 * 60 * 60
ten_year_from_now = Time.now + ten_year_in_seconds
# to update an existing object
bucket.objects.each do |obj|
begin
obj.copy_to(obj.key,
:cache_control => "max-age=#{ten_year_in_seconds}",
:expires => ten_year_from_now.httpdate)
rescue Exception => e
puts "Exception #{e}"
end
end
# reference : http://www.thehorrors.org.uk/snippets/aws-ruby-s3-set-content-expires-data/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment