# ENV["ASSETS_HOST"] = 'thing.cloudfront.net'
class Foo < ApplicationRecord
has_one_attached :thing
end
foo = Foo.first
# https://thing.cloudfront.net/rails/active_storage/blobs/etc..etc
proxy_url(foo.image)
# https://thing.cloudfront.net/rails/active_storage/representations/etc..etc
proxy_url(foo.image.variant(resize: '100x100'))
This is nowhere near perfect. Just gets the job done in a simple manner.
Inspiration from https://github.com/rails/rails/pull/34477
I also had to put the files in
app/lib
rather than at the root/lib
. Since Rails wasn't autoloading before boot in production, it was giving meNameError
.Still couldn't get Cloudfront to work with variants for some reason, so I ended up scrapping the monkey patch and leveraged ImageKit (referral link 🙏🏽), which is actually pretty dope. Easy to work with since it plugs directly into S3 and handles all the variant and cropping I was looking to do with ActiveRecord. I'm no longer using ActiveRecord variants.
Then in the API response, instead of using
url_for(image)
, I created an application helper that builds a unique url for ImageKit based on the blob key, so it hits S3 directly and bypasses the server.