Skip to content

Instantly share code, notes, and snippets.

@danblaker
Forked from fleveque/s3_folder_upload.rb
Last active August 9, 2019 17:39
Show Gist options
  • Select an option

  • Save danblaker/37b7f850bc45511b3dcd39001867e368 to your computer and use it in GitHub Desktop.

Select an option

Save danblaker/37b7f850bc45511b3dcd39001867e368 to your computer and use it in GitHub Desktop.
Upload folder to S3 recursively with ruby, multi threads and aws-sdk v2 gem, based on http://avi.io/blog/2013/12/03/upload-folder-to-s3-recursively/
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk-s3'
# Forked from https://gist.github.com/fleveque/816dba802527eada56ab
class S3FolderUpload
attr_reader :folder_path, :total_files, :s3_bucket, :include_folder, :base_path
attr_accessor :files
# Initialize the upload class
#
# folder_path - path to the folder that you want to upload
# bucket - The bucket you want to upload to
# base_path - Any portion of the folder_path that should be removed from the S3 key
# aws_key - Your key generated by AWS defaults to the environemt setting AWS_KEY_ID
# aws_secret - The secret generated by AWS
# region - The region for the AWS bucket
# include_folder - include the root folder on the path? (default: true)
#
# Examples
# => uploader = S3FolderUpload.new(folder_path: "some_route/test_folder", bucket: 'your_bucket_name')
#
def initialize(folder_path:, bucket:, base_path: nil, aws_key: ENV['AWS_KEY_ID'], aws_secret: ENV['AWS_SECRET'], region: ENV['AWS_REGION'], include_folder: true)
Aws.config.update({
region: region,
credentials: Aws::Credentials.new(aws_key, aws_secret)
})
@base_path = base_path
@folder_path = folder_path
@files = Dir.glob("#{folder_path}/**/*")
@total_files = files.length
@connection = Aws::S3::Resource.new
@s3_bucket = @connection.bucket(bucket)
@include_folder = include_folder
end
# public: Upload files from the folder to S3
#
# thread_count - How many threads you want to use (defaults to 5)
# verbose - Verbose info (default: false)
# simulate - Don't perform upload, just simulate it (default: false)
#
# Examples
# => uploader.upload!(20)
# true
# => uploader.upload!
# true
#
# Returns true when finished the process
def upload!(thread_count = 5, verbose = false, simulate = false)
threads = []
puts "Total files: #{total_files}... uploading (folder #{folder_path} #{include_folder ? '' : 'not '}included)"
thread_count.times do |i|
threads[i] = Thread.new {
until files.empty?
file = files.pop
next unless file
puts "uploading #{file}" if verbose
upload_file(file) unless File.directory?(file) || simulate
end
}
end
threads.each { |t| t.join }
end
private
def upload_file(file)
# Define destination path
s3_file_key = include_folder ? file.sub(/^#{base_path}\//, '') : file.sub(/^#{folder_path}\//, '')
obj = s3_bucket.object(s3_file_key)
obj.upload_file(file)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment