Skip to content

Instantly share code, notes, and snippets.

@ostinelli
Last active January 11, 2017 15:33
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 ostinelli/2dcfe213d8fa3173fe082a26cac4ad66 to your computer and use it in GitHub Desktop.
Save ostinelli/2dcfe213d8fa3173fe082a26cac4ad66 to your computer and use it in GitHub Desktop.
Batch update ACL permissions on AWS.
# add AWS credentials to ENV variables or modify the script to pass in credentials
require 'thread'
require 'aws-sdk'
# settings
region = 'my-region'
bucket = 'my-bucket'
prefix = 'my-prefix'
acl = "private"
parallel = 25
# init
queue = Queue.new
consumers = []
print "-----> populating queue... "
total_items = 0
s3 = Aws::S3::Resource.new(region: region)
bucket = s3.bucket(bucket)
bucket.objects(prefix: prefix).each do |object|
total_items += 1
queue << object
end
puts "#{total_items} objects added."
puts "-----> starting #{parallel} consumer threads"
$total_updated = 0
parallel.times do |thread_i|
consumers[thread_i] = Thread.new do
while queue.length > 0
$total_updated += 1
object = queue.pop
puts "[#{$total_updated}] #{object.key}"
object.acl.put(acl: "private")
end
end.tap { |t| t.abort_on_exception = true }
end
consumers.each { |c| c.join }
puts "-----> done updating #{$total_updated}/#{total_items} objects."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment