All threads / Manipulate photos already on S3 or Spaces

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Manipulate photos already on S3 or Spaces

Ryan Carter asked in Rails

Scenerio: User uploads a huge file to S3 or Spaces.

Question: How can I set up a job or worker to go into S3 or Spaces and resize that file at a later time, or even batch all of the days uploaded images to a max size and quality. I am trying to save space on my server not just create variations, so I want to replace the original file with the new "optimized" file.

Hi Ryan,

There's a few different approaches to this.

I'd either set up a after_create callback in your model, which would enqueue your resizer job.

class Photo < ApplicationRecord
  after_create :enqueue_resizer_job

    private
    def enqueue_resizer_job
      PhotoResizerJob.perform_later(id)
    end
end

Your job would then get the image uploaded to S3, resizing it, and rewriting it to S3. You could then set an attribute on your Photo model to true (resized = true) for instance.

Another way, could be to do this in a batch daily, hourly or whatever. For that I'd recommend the whenever gem (https://github.com/javan/whenever). That way you could do something like

Photo.where(resized: false).each do |photo|
  # resize photo and set resized to true
end

Jesper, thanks for the info.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,909+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.