lib/s3_html.rb
# 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
Upload files from the folder to S3
thread_count - How many threads you want to use (defaults to 5)



When finished the process succesfully: Returns key and public_url value
When finished the process unsuccesfully: Returns false TODO; need some specific error return
Upload files from the folder to S3 thread_count - How many threads you want to use ( defaults to 5 ) When finished the process succesfully : Returns key and public_url value When finished the process unsuccesfully : Returns false TODO ; need some specific error return
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
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
public: Upload files from the folder to S3
thread_count - How many threads you want to use (defaults to 5)
public : Upload files from the folder to S3 thread_count - How many threads you want to use ( defaults to 5 )
thread_count