Created
April 3, 2013 15:53
-
-
Save newtriks/5302459 to your computer and use it in GitHub Desktop.
Class to generate thumbnails for images in a dir stored in an Amazon S3 bucket.
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
#!/usr/bin/env ruby | |
################################################### | |
# | |
# Class to generate thumbnails for images in a dir | |
# stored in an Amazon S3 bucket. | |
# | |
# simon@newtriks.com - 10/2012 | |
# | |
################################################### | |
require 'image_sorcery' # Required for image conversion, needs ImageMagick installed | |
require 'aws/s3' # Required to interact with Amazon S3 | |
require 'fileutils' # Required for creating temp dir | |
require 'tmpdir' # Required for tempdir logic | |
class GenerateThumbnail | |
# TODO: Error catching | |
# TODO: Only convert if an image | |
def initialize(u_p, b) | |
connect_to_S3({ key: 'AWS-KEY-GOES-HERE', | |
secret: 'AWS-SECRET-GOES-HERE', | |
region: 's3-eu-west-1.amazonaws.com'}) | |
generate({ upload_path: u_p, | |
bucket: b}) | |
end | |
private | |
# Expects a key/secret, plus region which was added | |
# due to EU region issue: https://github.com/marcel/aws-s3/issues/4 | |
def connect_to_S3(options={}) | |
puts "Connection to amazon #{options.values}" | |
AWS::S3::DEFAULT_HOST.replace options[:region] | |
AWS::S3::Base.establish_connection!(:access_key_id => options[:key], :secret_access_key => options[:secret]) | |
end | |
# Expects and upload path and a bucket. | |
# 1. Creates a temp dir | |
# 2. Gets the contents of specified dir in bucket | |
# 3. Loops thorough each file (image) in dir & creates a thumbnail | |
def generate(options={}) | |
@tmp_path = Dir.mktmpdir | |
Dir.mkdir "#{@tmp_path}/t" | |
filenames = AWS::S3::Bucket.find(options[:bucket], :prefix => options[:upload_path]) | |
filenames.each do |fname| | |
filename = fname.key.scan(/[\w_.-]*?(?=\?)|[\w_.-]*$/)[0] | |
if filename.length > 1 | |
puts "Convert file: #{filename}" | |
open("#{@tmp_path}/#{filename}", 'w') do |file| | |
AWS::S3::S3Object.stream("#{fname.key}", options[:bucket]) do |chunk| | |
file.write chunk | |
end | |
end | |
image = Sorcery.new("#{@tmp_path}/#{filename}") | |
image.convert("#{@tmp_path}/t/thumbnail_#{filename}", :thumbnail => "128x96") | |
end | |
end | |
uploadFilesToBucket({ path: @tmp_path, | |
upload_path: options[:upload_path], | |
bucket: options[:bucket]}) | |
FileUtils.remove_entry_secure @tmp_path | |
end | |
# Expects a dir with thumbs, upload path and bucket | |
# 1. Gets all the files from the temp dir | |
# 2. Loops through the files and uploads to specified dir in bucket. | |
def uploadFilesToBucket(options={}) | |
puts "Upload files to Amazon S3 bucket: #{options[:bucket]}." | |
filenames = Dir.entries("#{@tmp_path}/t/").select { |file| /.jpg|.pdf|.png|.JPG|.PNG/ =~ file } | |
filenames.each do |name| | |
puts "Uploading #{@tmp_path}/t/#{name} to #{options[:upload_path]}/#{name}" | |
AWS::S3::S3Object.store("#{options[:upload_path]}/#{name}",open("#{@tmp_path}/t/#{name}").read,options[:bucket],:access => :public_read) | |
end | |
end | |
end | |
# Generate thumbnail for images in a directory | |
GenerateThumbnail.new("my_path/to/a/directory", "my-bucket") | |
# END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment