Skip to content

Instantly share code, notes, and snippets.

@alex-tan
Created February 12, 2016 20:32
Show Gist options
  • Save alex-tan/6b01cf4cf7c4b31ddd31 to your computer and use it in GitHub Desktop.
Save alex-tan/6b01cf4cf7c4b31ddd31 to your computer and use it in GitHub Desktop.
Rake Task to Update AWS Content Dispositions of All Files in a Bucket
namespace :aws do
desc 'Update Dispositions'
task :update_dispositions => :environment do
bucket_name = ENV['BUCKET']
s3 = Aws::S3::Resource.new
bucket = s3.bucket(bucket_name)
bucket.objects.each do |obj|
key = obj.key
original_key = key + ".original"
# Print what file we're moving.
puts key
# Move the file to different location.
obj.move_to({ bucket: bucket_name, key: original_key})
# Copy it back to its original location with the new content disposition.
bucket.object(original_key).copy_to({ bucket: bucket_name, key: key }, {
metadata_directive: 'REPLACE',
content_disposition: 'attachment'
})
# Delete the copy.
bucket.object(original_key).delete
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment