Skip to content

Instantly share code, notes, and snippets.

@swapnilchincholkar
Created July 27, 2014 17:25
Show Gist options
  • Save swapnilchincholkar/1b8aa6ccd719a7a54ef1 to your computer and use it in GitHub Desktop.
Save swapnilchincholkar/1b8aa6ccd719a7a54ef1 to your computer and use it in GitHub Desktop.
Ruby code to copy images from or within 3 bucket, without reprocessing
module AssignImagesS3
def assign_images(src_parent_obj, dest_parent_obj)
# 1. get parent image object
src_parent_obj.images.each do |src_img|
# 2. create new image object and save it without any attachment
dest_img = dest_parent_obj.images.create(is_processed: true)
# 3. While assigning src image attributes to dest obj,
# assign attribute 'uploading_via_copy' to true so
# it will not process image assigned to it
dest_img.uploading_via_copy = true
# 4. This step will assign src object attributes to dest obj
# & will provide new image url for this new obj,
# without processing assigned image
dest_img.image = src_img.image.clone
# 5. Copy images to this new object's location within s3 bucket
copy_files(src_img.image.path, dest_img.image.path, PAPERCLIP_S3[:bucket])
# 6. Save new object
dest_img.save
end unless src_parent_obj.images.blank?
end
# This method takes parameters as
# 1. source image path => from where to fetch images
# 2. destination_image_path => images will be saved with this path under bucket
# 3. bucket_name => within/from which we need to copy images
# we pass path of new and old images for processing
# paperclip config has path option as "path, as
# ':class/:attachment/:id/:style.:extension'
# Eg. "images/images/50f668b758507c0002000349/original.jpg"
def copy_files(source_image_path, destination_image_path, bucket_name)
#create s3 object, assign bucket name to it
s3 = AWS::S3.new
bucket = s3.buckets[bucket_name]
# SOURCE path processing
# returns array of strings splitted by '/'
image_info = source_image_path.split('/')
# remove file name, which is at the
# end after splitting
image_info.pop
# join '/' to get path upto object_id
# i.e "images/images/50f668b758507c0002000349/"
image_prefix = image_info.join('/') + '/'
# DEST path processing
dest_image_info = destination_image_path.split('/')
# remove file name, which is at the
# end after splitting
dest_image_info.pop
dest_image_prefix = dest_image_info.join('/')
bucket.objects.with_prefix(image_prefix).each do |photo|
#create destination bucket url with new destination_object_id
# path of image within the bucket
# i.e "/images/images/5114dcc27260f70002000025/original.jpg"
key = photo.key
# split key i.e path of img using '/'
info = key.split('/')
# get image name from it
name = info.last
# generate destination file name by joining dest image id with src image name
# this is nothing but image path under a bucket
destination_file_name = dest_image_prefix + '/' + name
# copy files from one directory to other,
# with public-read permissions
bucket.objects[destination_file_name].copy_from(photo.key, {acl: 'public-read'})
end if bucket.exists?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment