Manipulate photos already on S3 or Spaces
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