Last active
July 27, 2023 14:09
-
-
Save rahul100885/d788a041e2c36fd40f24 to your computer and use it in GitHub Desktop.
Ruby script to add Metadata ( Cache Control and Expires) header to existing S3 Objects
This file contains hidden or 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
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