If ever you'll find yourself having problems with displaying a paperclip attachment stored in a Non-US AWS S3 bucket, please try this solution.
#Gemfile
gem 'paperclip' #3.5.1
gem 'rmagick' #2.13.2
gem 'aws-sdk' #1.18.0
gem 'rails_admin' #0.0.1
So, the trouble was, the image doesn't display on a show and edit page because it kept calling the default endpoint of s3.amazonaws.com
.
Solution:
-
Install dot_env gem to isolate all constants and setup related data
-
gem install dot_env
-
Create your
.env
file as such, using the suggested AWS constants:AWS_ACCESS_KEY_ID: accesskeyid
AWS_SECRET_ACCESS_KEY: mysecretaccesskey
AWS_DEFAULT_REGION: ap-southeast-2
BUCKET: mybucketname
-
Your default region is the region where you assigned your bucket to. You can find the list of regions here: Regions and Endpoints. Also take note of the endpoint here.
-
Append a config for paperclip inside your environment file (
config/environments/development.rb
, etc.) where yours3_host_name
is your endpoint found in the Regions and Endpoints AWS document.config.paperclip_defaults = { :storage => :s3,
:s3_credentials => {
:bucket => ENV['BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']},
:s3_host_name => 's3-ap-southeast-2.amazonaws.com'} -
Modify your
rails_admin.rb
. If you have a model that has an image field which is a Paperclip object, you can do this for the display (aside from simply usingthumb_method :style
)field(:image) do
pretty_value do
bindings[:view].tag(:img, { :src => bindings[:object].image.url(:style) })
end
end
I spent quite some time experimenting with the correct settings. I hope this saves someone else's time! :)
Author
Maricris Nonato
I turn tech ideas into websites using Ruby. I'm a budding Buddhist practitioner, passionate about health & fitness, a certified yoga teacher, and a cat fanatic!
Recent posts
Tags
- cats
- yoga
- blood pressure
- elecom
- scrum
- coderkitty
- RailsGirls
- workstation
- creativity
- live book signing
- healthy living
- lowering blood pressure
- crafting
- movies
- reviews
- new year
- blessings
- faith
- quotes to live by
- travelers notebook
- Bujo
- planners
- Monday Motivation
- himalayan salt lamp
- productivity
- life tips
- happiness
- success
- ruby
- digital ocean
- regex
- programming
- humor
- music
- pyromusical
- sphinx
- mac
- games
- musings
- yogi
- namaste
- photography
- hobby
- slimming
- sendmail
- lifestyle
- memes
- open letter
- sports
- shooting
- events
- meetups
- tech talks
- rails
- datatables
- sorting
- cycling
- COVID-19
- wishlist
- xmas
- tradition
- song lyrics
- life lessons
- poems
- short story
- food
- living life
- nginx
- technology
- youtube
- my Nyey
- meditation
- new normal
- jump rope
- two-wheel journey
- planner girl
- rubyist
- culture
- Filipino
- mental health
- counseling
- self-care
Advertise
Newsletter
Instagram Feed
Links
I'm sharing with you P150 gift from GoFutureFresh! Click here to accept my gift.