Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save salahuddin-rakib/272b013b482371d4765608f7b8e11e03 to your computer and use it in GitHub Desktop.
Save salahuddin-rakib/272b013b482371d4765608f7b8e11e03 to your computer and use it in GitHub Desktop.
# Make a file named image_upload_to_s3.rb under services folder of your ROR project:
image_upload_to_s3.rb file:
require "aws-sdk-s3"
class ImageUploadToS3
attr_reader :object
def initialize(object)
@object = object
end
def upload_file(file_path)
puts "------------- Starting to upload: #{file_path.split('/').last(4).join('/')} ---------------"
@object.upload_file(file_path)
true
rescue Aws::Errors::ServiceError => e
puts "Couldn't upload file #{file_path.split('/').last(4).join('/')} to #{@object.key}. Here's why: #{e.message}"
false
end
end
# Now create a rake file named image_upload_to_s3.rake under lib/tasks folder. If those folder isn't exist then create those folder first:
image_upload_to_s3.rake file:
require 'fileutils'
desc 'Existing image upload to s3 bucket.'
task image_upload: :environment do
path_to_error_file = "#{Rails.root}/public/uploads/error_file.txt" # error file path
File.delete(path_to_error_file) if File.exist?(path_to_error_file)
# Get relative paths of all files you want to upload on aws. my files structure was: public/uploads/folder_name/folder_name/folder_name/all_files
path = Rails.root.to_s + "/public/uploads/**/**/**/*"
#################################################################################
####### Dir.glob(path) gives us all the paths on that specific locations ########
#################### So we need to filter image file paths ######################
#################################################################################
files = Dir.glob(path).filter { |f| f.split('/').last.match(/.jpg|.jpeg|.gif|.png/) } # Read all image file paths
p "\nImage file paths are: #{files}\n"
# AWS s3 bucket name:
bucket_name = ENV.fetch('S3_BUCKET_NAME')
# Save errors on error_file:
e_file = File.open(path_to_error_file, 'w')
errors = []
# Loop through all image file paths and upload to AWS:
files.each do |file_path|
# object_key = "uploads/#{file_path.split('/').last(4).join('/')}"
###################################################################################
####### Object key will be the name with path, that you want to save on s3 ########
###################################################################################
object_key = "uploads/#{file_path.split('/').last(4).join('/')}"
# Initialize s3 uploader with aws object:
uploader = ImageUploadToS3.new(Aws::S3::Object.new(bucket_name, object_key))
# if write_file_to_local(file_path) # Save file to local
if uploader.upload_file(file_path) # Upload to AWS
puts "File: #{file_path} successfully uploaded to #{bucket_name}:#{object_key}."
else
errors << file_path
puts "File: #{file_path} upload failed to #{bucket_name}:#{object_key}."
end
end
# Write upload failed file paths.
e_file.write(errors)
end
def write_file_to_local(path)
file_path = "#{Rails.root}/public/uploads_1" + '/' + "#{path.split('/').last}" # Local file path in where you want to save images
dirname = File.dirname(file_path) # Fetch directory names in from the file_path
unless File.directory?(dirname) # Checking if directory exist or not?
FileUtils.mkdir_p(dirname) # Create all the directories recursively if not exist
end
puts "------------- Starting to write in local: #{file_path} folder ---------------"
file = File.new(file_path, 'wb')
file.write(File.open(path, 'rb') {|file| file.read })
true
rescue => e
puts "Couldn't write file #{file_path.split('/').last(4).join('/')} to local. Here's why: #{e.message}"
false
end
require 'fileutils'
desc 'Existing image upload to s3 bucket.'
task image_upload: :environment do
path_to_error_file = "#{Rails.root}/public/uploads/error_file.txt" # error file path
File.delete(path_to_error_file) if File.exist?(path_to_error_file)
# Get relative paths of all files you want to upload on aws. my files structure was: public/uploads/folder_name/folder_name/folder_name/all_files
path = Rails.root.to_s + "/public/uploads/**/**/**/*"
#################################################################################
####### Dir.glob(path) gives us all the paths on that specific locations ########
#################### So we need to filter image file paths ######################
#################################################################################
files = Dir.glob(path).filter { |f| f.split('/').last.match(/.jpg|.jpeg|.gif|.png/) } # Read all image file paths
p "\nImage file paths are: #{files}\n"
# AWS s3 bucket name:
bucket_name = ENV.fetch('S3_BUCKET_NAME')
# Save errors on error_file:
e_file = File.open(path_to_error_file, 'w')
errors = []
# Loop through all image file paths and upload to AWS:
files.each do |file_path|
# object_key = "uploads/#{file_path.split('/').last(4).join('/')}"
###################################################################################
####### Object key will be the name with path, that you want to save on s3 ########
###################################################################################
object_key = "uploads/#{file_path.split('/').last(4).join('/')}"
# Initialize s3 uploader with aws object:
uploader = ImageUploadToS3.new(Aws::S3::Object.new(bucket_name, object_key))
# if write_file_to_local(file_path) # Save file to local
if uploader.upload_file(file_path) # Upload to AWS
puts "File: #{file_path} successfully uploaded to #{bucket_name}:#{object_key}."
else
errors << file_path
puts "File: #{file_path} upload failed to #{bucket_name}:#{object_key}."
end
end
# Write upload failed file paths.
e_file.write(errors)
end
def write_file_to_local(path)
file_path = "#{Rails.root}/public/uploads_1" + '/' + "#{path.split('/').last}" # Local file path in where you want to save images
dirname = File.dirname(file_path) # Fetch directory names in from the file_path
unless File.directory?(dirname) # Checking if directory exist or not?
FileUtils.mkdir_p(dirname) # Create all the directories recursively if not exist
end
puts "------------- Starting to write in local: #{file_path} folder ---------------"
file = File.new(file_path, 'wb')
file.write(File.open(path, 'rb') {|file| file.read })
true
rescue => e
puts "Couldn't write file #{file_path.split('/').last(4).join('/')} to local. Here's why: #{e.message}"
false
end
require "aws-sdk-s3"
class ImageUploadToS3
attr_reader :object
def initialize(object)
@object = object
end
def upload_file(file_path)
puts "------------- Starting to upload: #{file_path.split('/').last(4).join('/')} ---------------"
@object.upload_file(file_path)
true
rescue Aws::Errors::ServiceError => e
puts "Couldn't upload file #{file_path.split('/').last(4).join('/')} to #{@object.key}. Here's why: #{e.message}"
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment