DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rake Task To Set All S3 Files Public_read

07.14.2008
| 6217 views |
  • submit to reddit
        //  If you ever need to make sure all your Amazon S3 files are set to public_read, here's a rake task


namespace :s3 do
  desc "Make all objects in S3 public_read"
  task :make_public_readable do
    require 'aws/s3'
    # you might have this setup as env vars, doesn't work for me as i have more than one AWS account
    AWS::S3::Base.establish_connection!(:access_key_id => '',:secret_access_key => '')    
    
    marker = ""
    
    loop do
      objects = AWS::S3::Bucket.objects('your_bucket', :marker=>marker, :max_keys=>100)
      puts "found #{objects.size} objects"
    
      break if objects.size == 0
    
      marker = objects.last.key
      puts "new marker is \"#{marker}\""
    
      public_grant = AWS::S3::ACL::Grant.grant :public_read
  
      objects.each do |o|
        if not o.acl.grants.include? public_grant
          puts "\"#{o.key}\" does not include public_read"
          o.acl.grants << public_grant
          o.acl(o.acl)
        end
      end
    end
  end
  
end