Last active
January 11, 2017 15:33
-
-
Save ostinelli/2dcfe213d8fa3173fe082a26cac4ad66 to your computer and use it in GitHub Desktop.
Batch update ACL permissions on AWS.
This file contains 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
# 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