I would like to encrypt a file before storing it on S3. I'm using Shrine for uploads, and Lockbox for encryption. Shrine#upload
takes an IO-like object, but the Lockbox documention excludes some steps that are needed for setting this up. It simply says to do something like:
class Photo < ApplicationRecord
belongs_to :imageable, polymorphic: true
validates_presence_of :image
lockbox = Lockbox.new(key: ENV['LOCKBOX_MASTER_KEY'])
PhotoUploader.upload(lockbox.encrypt_io(image), :store)
end
In this case, image
is the undefined local variable.
If the file is user uploaded (and whitelisted in the controller), how would I access it in the model so that I can perform this operation when it is saved?
You could move this into a
before_save
method. You could also change it toafter_save
if you wanted to do it then (it was a little unclear from your question).