简体   繁体   中英

Rails 2 paperclip with s3 slow image upload

I am working on rails 2 application with paperclip gem to upload image on s3. some times it take long time to upload an image and some time it uploads very fast.

So problem is related to server space on s3 or my internet speed?

The answer to your question is subjective to both code and internet connection..

for example:-

  1. if you are using multiple styles such as:-

    :styles => { :thumb => "100x100#", :small => "150x150>", :medium => "200x200" }

then..it can take time...as u are converting the images at runtime..thats why we have delayed_paperclip to get only the styles that we really need on other upcoming page and convert the rest of the styles in the background using redis .

  1. Sometimes when u add multiple files during upload..it can take time as each uploaded image need to be converted first using above mentioned styles before dumping in the bucket at s3.So handle article has_many :photos carefully during multiple image uploads and limit multiple uploads to 3 at a time using both js and server side validation
  2. (This is what i feel)You need to optimise your image#create method and remove all unwanted code and emphasis only on creating/saving the object,rest of the things can be handled by after_create or observer .use rails profiler to understand and optimise the code...

Check out my post on a similar question.

Basically, use Threads to make things appear faster. It won't actually make things faster, it will just run code in a separate process and won't take up time in the main code execution.

When the next page is loaded, none of the images will show unless you refresh the page again after a few seconds.

If you're saving one image at a time.

# cats_controller.rb
def create
  @cat = Cat.find params[:id]
  mutex = Mutex.new

  Thread.new do
    mutex.synchronize do
      @cat.update cat_params
    end
  end
end

If you're uploading many images at a time

# cat.rb
def files=(array = [])
  threads = []
  semaphore = Mutex.new

  array.each do |f|
    threads << Thread.new do
      semaphore.synchronize do
        images.create file: f
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM