简体   繁体   中英

Rails Paperclip Heroku AWS DIsplay Image issue

I set up paperclip in a rails app and this worked fine locally and on heroku, however the images uploaded in posts were only saved in heroku for a short space of time. I set up an AWS account and created a bucket, and followed through the documentation to link my rails app to AWS to display images uploaded with paperclip. I have attached code snippets below. The images seem to be uploaded to AWS fine, however when I create a post, it says my post has been created successfully, however the image does not display, it just displays as a broken link. When I click image properties, it hs the AWS S3 url and upon looking in heroku logs, there are no known issues. I do not know why the image is not displaying.

config/environments/production.rb file:

 # sets paperclip to upload images to Amazon S3 # Variables directed to heroku via the command line for pw etc config.paperclip_defaults = { storage: :s3, s3_credentials: { bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION'), } } 

post.rb model file:

 class Post < ApplicationRecord extend FriendlyId friendly_id :title, use: [:slugged, :finders] has_attached_file :image, styles: { medium: "600x", thumb: "100x" } validates_attachment_content_type :image, content_type: /\\Aimage\\/.*\\z/ end 

show.html.erb file:

 <div class="image"> <%= image_tag @post.image.url(:medium) %> </div> 

I have also set up heroku correctly using the following:

$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
$ heroku config:set AWS_REGION=your_aws_region

Any help would be appreciated.

Many thanks

As per Judd's answer for Paperclip, S3, Heroku: Missing Image , it was because I needed to state the s3_host_name in the paperclip default settings:

 # sets paperclip to upload images to Amazon S3 # Variables directed to heroku via the command line for pw etc config.paperclip_defaults = { storage: :s3, s3_host_name: 's3-eu-west-1.amazonaws.com', s3_credentials: { bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION'), } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM