Skip to content

Instantly share code, notes, and snippets.

@wuputah
Created September 29, 2008 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuputah/13704 to your computer and use it in GitHub Desktop.
Save wuputah/13704 to your computer and use it in GitHub Desktop.
Grants public read to an S3 bucket
#!/usr/bin/ruby
require 'rubygems'
require 'aws/s3'
include AWS::S3
AWS::S3::Base.establish_connection!(
:access_key_id => 'abc',
:secret_access_key => '123'
)
bucket = Bucket.find('some-bucket')
# cached here so we don't create it 10**6 times
public_read_grant = ACL::Grant.grant(:public_read)
objects = bucket.objects(:marker => ARGV.shift || '')
end_marker = ARGV.shift || nil
until objects.size == 0
objects.each do |object|
begin
unless object.acl.grants.include?(public_read_grant)
object.acl.grants << public_read_grant
object.acl(object.acl)
puts "Made public: #{object.key}"
else
puts "Already public: #{object.key}"
end
# for some reason, despite S3 telling us there was
# a key here a second ago, they have a tendency to
# disapppear every once in a while
rescue AWS::S3::NoSuchKey
puts "AWS::S3::NoSuchKey: #{object.key}"
rescue Errno::ECONNRESET
puts "ECONNRESET while modifying ACL. Retrying..."
retry
end
end
# reload bucket objects starting from last current object
begin
objects = bucket.objects(:marker => bucket.objects.last.key)
rescue Errno::ECONNRESET
puts "ECONNRESET while getting more objects. Retrying..."
retry
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment