Skip to content

Instantly share code, notes, and snippets.

@indrode
Last active April 8, 2016 09:26
Show Gist options
  • Save indrode/9f897a8fd6e257da9260ee4ed14e64cf to your computer and use it in GitHub Desktop.
Save indrode/9f897a8fd6e257da9260ee4ed14e64cf to your computer and use it in GitHub Desktop.
# gem 'aws-sdk', '~> 2'
# Aws.config.update(region: 'eu-central-1', credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']))
# s3 = Aws::S3::Client.new
class S3OwnerPics
BUCKET_PREFIX = ''
attr_reader :property
def initialize(property)
@property = property
end
def move
source_bucket_key = property.owner_pic.path.match(/owner_pic\/(\w+)\//)[1]
source_bucket = "#{BUCKET_PREFIX}/images/owners/owner_pic/#{source_bucket_key}"
bucket = Aws::S3::Bucket.new(source_bucket)
['', '_small', '_tiny', '_medium'].each do |version|
object = bucket.object("#{property.uid}#{version}.jpg")
object.copy_to("#{BUCKET_PREFIX}/images/owners/owner_pic/#{hashed_path(property)}/#{property.uid}#{version}.jpg", acl: 'public-read')
end
rescue
puts "Couldn't move owner pics for [Property:#{property.id}]"
end
def uid_md5
Digest::MD5.hexdigest(property.uid.to_s)
end
def hashed_path
"#{uid_md5[0]}/#{uid_md5[1]}/#{uid_md5[2]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment