Skip to content

Instantly share code, notes, and snippets.

@jrust
Created October 17, 2012 23:21
Show Gist options
  • Save jrust/3908968 to your computer and use it in GitHub Desktop.
Save jrust/3908968 to your computer and use it in GitHub Desktop.
Resize thumbnails on S3 #amazon #scripts
require 'rubygems'
require 'aws-sdk'
require 'mini_magick'
aws_access_key_id = 'access-key'
aws_secret_access_key = 'secret'
bucket_name = 'bucket'
s3 = AWS::S3.new :access_key_id => aws_access_key_id, :secret_access_key => aws_secret_access_key
target_bucket = s3.buckets[bucket_name]
# Set a prefix to copy just a portion of the bucket.
# Useful for limiting copy or spinning off several processes copying different parts of the bucket simultaneously
prefix = 'path/to/images/1'
# Set cache control if desired
cache_control = 'max-age=29030400'
# Set ACL
acl = :public_read
target_bucket.objects.with_prefix(prefix).each do |obj|
image = MiniMagick::Image.read(obj.read)
image.strip
image.interlace "Plane"
image.resize "228x300>"
obj.write(image.to_blob, acl: acl, cache_control: cache_control, content_type: image.mime_type)
puts obj.key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment