class S3Html::S3FolderUpload
Attributes
files[RW]
folder_path[R]
s3_bucket[R]
total_files[R]
Public Class Methods
new(extract_point, uniq_path, account_id)
click to toggle source
# File lib/s3_html.rb, line 60 def initialize(extract_point, uniq_path, account_id) @folder_path = extract_point << uniq_path @files = Dir.glob("#{folder_path}/**/*") @total_files = files.length @extract_point = extract_point @uniq_path = uniq_path @account_id = account_id end
Public Instance Methods
upload!(thread_count = 5)
click to toggle source
public: Upload files from the folder to S3
thread_count - How many threads you want to use (defaults to 5)
Examples
=> uploader.upload!(20) true => uploader.upload! true
When finished the process succesfully: Returns key and public_url value When finished the process unsuccesfully: Returns false TODO; need some specific error return
# File lib/s3_html.rb, line 81 def upload!(thread_count = 5) result = nil file_number = 0 mutex = Mutex.new threads = [] thread_count.times do |i| threads[i] = Thread.new { until files.empty? mutex.synchronize do file_number += 1 Thread.current["file_number"] = file_number end file = files.pop rescue nil next unless file path = file puts "[#{Thread.current["file_number"]}/#{total_files}] uploading..." data = File.open(file) if File.directory?(data) data.close next else prefix = "htmls/#{@account_id}/" << @uniq_path Aws.config.update({ region: S3_REGION, credentials: Aws::Credentials.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) }) s3 = Aws::S3::Resource.new(region:S3_REGION) obj = s3.bucket(S3_BUCKET_NAME).object(prefix + path.sub(@extract_point, "")) if obj.upload_file data, {acl: 'public-read'} result = { :key_url => obj.key, :public_url => obj.public_url } end data.close end end } end threads.each { |t| t.join } final_result = {} if result final_result[:key_url] = result[:key_url].split(@uniq_path)[0] + @uniq_path final_result[:public_url] = result[:public_url].split(@uniq_path)[0] + @uniq_path + "/index.html" else final_result = false end return final_result end