Created
February 12, 2016 23:23
-
-
Save mcfadden/b1e564f3323f98720ff2 to your computer and use it in GitHub Desktop.
Change the storage class of every item in an AWS bucket
This file contains hidden or 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
# Ever want to change the storage class of every item in an AWS bucket? | |
# Requirements: | |
# Gems: | |
# fog-aws - http://fog.io | |
# Usage: | |
# All items in the bucket: | |
# set_storage_class_for_s3_files("REDUCED_REDUNDANCY") | |
# Only items in the 'foobar' folder | |
# set_storage_class_for_s3_files("STANDARD", "my-s3-bucket", "/foobar") | |
# Only the first 10,000 items | |
# set_storage_class_for_s3_files("STANDARD", "my-s3-bucket", "", 10000) | |
AWS_BUCKET = "my-s3-bucket" | |
AWS_ACCESS_KEY = "MyAWSAccessKey" | |
AWS_SECRET_KEY = "SuperSecretAWSKey" | |
require 'fog/aws' # This can be a super slow require. 15+ seconds. Be patient. | |
def set_storage_class_for_s3_files(new_storage_class, bucket = AWS_BUCKET, path = "", limit = false) | |
raise "Unknown storage class" unless ["STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA"].include? new_storage_class | |
aws = Fog::Storage::AWS.new( { aws_access_key_id: AWS_ACCESS_KEY, aws_secret_access_key: AWS_SECRET_KEY, path_style: true } ) | |
directory = aws.directories.get(bucket, prefix: path) | |
total_count = 0 | |
updated_count = 0 | |
files = directory.files.all | |
files.each_with_index do |file, index| | |
break if limit && index >= limit | |
total_count += 1 | |
unless file.storage_class == new_storage_class | |
file.storage_class = new_storage_class | |
updated_count += 1 | |
end | |
if index > 0 && index % 1000 == 0 | |
puts "*" * 50 | |
puts "#{Time.now.strftime('%D %T')}" | |
puts "Processed #{total_count} items" | |
puts "Updated count: #{updated_count}" | |
end | |
end | |
puts "*" * 50 | |
puts "Completed At: #{Time.now.strftime('%D %T')}" | |
puts "Total count: #{total_count}" | |
puts "Updated count: #{updated_count}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment