Skip cache storage but upload all versions/derivatives of file at once

Hello,

I’m looking for a way to upload the original image and all the versions I have defined, all at once, to permanent storage. That is:

  • I’d like to bypass temporary (cache) storage, since I have no need for it
  • I don’t want to upload the original image, and then have to download it just to generate the different versions (small, medium, large, etc), I’d like to upload everything in one go, as it’s cheaper (no need to pay for an extra read access for each file).

Is it possible to do that?

I’m using Shrine 2.19.3, as this is the latest version from RubyGems, so I assumed it’s the most recent stable version.

Thank you!

  1. On Shrine 2.x it’s not straightforward to disable caching, but on Shrine 3.0 beta you can configure this:
Shrine.plugin :model, cache: true
  1. This is not straightforward on 2.x either with versions plugin, on 3.0 with derivatives plugin you can do it easily, for example:
Shrine.plugin :derivatives
thumbnails = ThumbnailProcessor.call(file)

photo.image = file
photo.add_derivatives(thumbnails)
photo.save
class ThumbnailProcessor
  def self.call(image)
    magick = ImageProcessing::MiniMagick.source(image)

    {
      large:  magick.resize_to_limit!(800, 800),
      medium: magick.resize_to_limit!(500, 500),
      small:  magick.resize_to_limit!(300, 300),
    }
  end
end

Shrine 3.0 is basically done now and is actually waiting to be tested, so I’d recommend giving 3.0.0.beta3 a go. I’ve just updated the website. If you’re already using Shrine 2.x in production, you can see the release notes and the upgrading guide.

Hi @janko,

Thank you for the answer!

This line didn’t work for me though, I got this error:

NoMethodError (undefined method `add_derivatives' for #...)

The model has a method called image_derivatives! but not a method called add_derivatives. I got it working using this, though:

# This works
photo.image_attacher.add_derivatives(thumbnails)