Skip to content

Instantly share code, notes, and snippets.

@tovbinm
Last active December 20, 2015 22:39
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 tovbinm/6207114 to your computer and use it in GitHub Desktop.
Save tovbinm/6207114 to your computer and use it in GitHub Desktop.
S3 Resize images
require 'aws/s3'
require 'RMagick'
include AWS::S3
include Magick
AWS::S3::Base.establish_connection!(
:access_key_id => 'key',
:secret_access_key => 'secret'
)
bucket = 'bucket_name';
marker = ''
loop do
objects = Bucket.objects(bucket, :marker=>marker, :max_keys=>1000)
break if objects.size == 0
marker = objects.last.key
objects.each do |obj|
fn = obj.key
if fn.include?('/') then
next
end
puts "#{fn}"
image = obj.value
begin
mimage = Magick::Image.from_blob(image).first
rescue
image = Base64.decode64(image)
mimage = Magick::Image.from_blob(image).first
#Fix image file encoding
S3Object.store(fn, image, bucket, :use_ssl => false)
end
#Resize to 42px and 64px
thumb42 = mimage.resize_to_fit(42).to_blob
thumb64 = mimage.resize_to_fit(64).to_blob
S3Object.store(fn, thumb42, bucket+'/42', :use_ssl => false)
S3Object.store(fn, thumb64, bucket+'/64', :use_ssl => false)
GC.start
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment