Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I get avatar.variant(resize: “200x200”) on Rails 5 to work using aws on production?

Luis Daniel Fonseca asked in General

I can't get user.avatar.variant(resize: "200x200") to work while using a cloud provider in production because it uses my website path instead of aws.

<%= image_tag user.avatar.variant(resize: "200x200") %> 

# Current Output
<img src="https://mywebsite.com/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3f8b21aebb9ca4fc9fe9043a1acc3e77e999319a/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lLTVRCNE1UQUdPZ1pGVkE9PSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--6b94a1495d331d40bee31fadf0e8dc9323a7e1bc/image.jpg">

# Should be
<img src="aws.com/img.jpg">

I need to user.avatar.service_url.split("?").first to get the right path for the img but when I do this, I can't use .variant() because that path is a string.

# this works but can't resize    
<%= image_tag user.avatar.service_url.split("?").first %> 

Any idea how I overcome this.

Thanks!

Reply

ActiveStorage URLs always go through your Rails app. That's how they generate the resize 200x200 on the fly without precomputing it. That's the primary downside to using ActiveStorage right now.

In a future version of Rails, they will have an option to use direct urls to AWS, but you can't do that right now.

If you want this feature today, you'd be better off using Shrine for file uploads.

Reply

Hey Chris! Thanks a lot for getting back to me.

Do you think it would make sense to implement like a post-process user images feature?

Something like this: https://www.freecodecamp.org/news/how-to-post-process-user-images-programmatically-with-rails-amazon-s3-including-testing-c72645536b54/

While they release that option of using direct urls to AWS on Active Storage.

The thing is that I already implemented the basic features for active storage, and I already had some users uploading their profile picture.

Thanks again!

Reply

Yeah, that would probably work fine for now. I'm not sure when Rails 6.1 will come out but it might be a little while so you'll probably need a solution like this until then. This basically replicates what Shrine would do for you.

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.