Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mcfadden
Created February 12, 2016 23:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfadden/b1e564f3323f98720ff2 to your computer and use it in GitHub Desktop.
Save mcfadden/b1e564f3323f98720ff2 to your computer and use it in GitHub Desktop.
Change the storage class of every item in an AWS bucket
# 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